Last active
June 26, 2016 12:24
-
-
Save Nazmul56/da11a60e17d270f4f779b9dfb478b2ac to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" | |
xmlns:ads="http://schemas.android.com/apk/res-auto" | |
android:layout_width="match_parent" | |
android:layout_height="match_parent" | |
android:orientation="vertical" | |
android:fitsSystemWindows="true"> | |
<com.google.android.gms.ads.NativeExpressAdView | |
android:id="@+id/adView" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
ads:adSize="FULL_WIDTHx80" | |
ads:adUnitId="@string/ad_unit_id" | |
android:visibility="gone" | |
android:background="@color/btnGray" | |
android:layout_gravity="center" | |
> | |
</com.google.android.gms.ads.NativeExpressAdView> | |
<com.google.android.gms.ads.NativeExpressAdView | |
android:id="@+id/adViewLand" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
ads:adSize="FULL_WIDTHx80" | |
ads:adUnitId="@string/ad_unit_id" | |
android:visibility="gone" | |
android:background="@color/btnGray" | |
android:layout_gravity="center" | |
> | |
</com.google.android.gms.ads.NativeExpressAdView> | |
<ListView | |
android:id="@+id/custom_list" | |
android:layout_width="fill_parent" | |
android:layout_height="fill_parent" | |
android:divider="@null" | |
style="@style/listStyleNoHighlight" | |
android:focusable="false" | |
android:visibility="gone" | |
android:paddingTop="4dp"/> | |
<include | |
android:id="@+id/progressBarHolder" | |
layout="@layout/listview_loading"/> | |
</LinearLayout> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.weblinkstech.teluguhdvideosongs.wordpress.ui; | |
import android.annotation.SuppressLint; | |
import android.app.Activity; | |
import android.content.Intent; | |
import android.content.res.Configuration; | |
import android.os.AsyncTask; | |
import android.os.Bundle; | |
import android.support.v4.app.Fragment; | |
import android.support.v7.widget.SearchView; | |
import android.support.v7.widget.SearchView.OnQueryTextListener; | |
import android.util.Log; | |
import android.view.Display; | |
import android.view.LayoutInflater; | |
import android.view.Menu; | |
import android.view.MenuInflater; | |
import android.view.MenuItem; | |
import android.view.View; | |
import android.view.View.OnAttachStateChangeListener; | |
import android.view.ViewGroup; | |
import android.widget.AbsListView; | |
import android.widget.AbsListView.OnScrollListener; | |
import android.widget.AdapterView; | |
import android.widget.AdapterView.OnItemClickListener; | |
import android.widget.LinearLayout; | |
import android.widget.ListView; | |
import android.widget.RelativeLayout; | |
import android.widget.Toast; | |
import com.google.android.gms.ads.NativeExpressAdView; | |
import com.weblinkstech.teluguhdvideosongs.MainActivity; | |
import com.weblinkstech.teluguhdvideosongs.R; | |
import com.weblinkstech.teluguhdvideosongs.util.Helper; | |
import com.weblinkstech.teluguhdvideosongs.wordpress.PostItem; | |
import com.weblinkstech.teluguhdvideosongs.wordpress.WordpressGetTask; | |
import com.weblinkstech.teluguhdvideosongs.wordpress.WordpressGetTaskInfo; | |
import java.io.UnsupportedEncodingException; | |
import java.net.URLEncoder; | |
/** | |
* This activity is used to display a list of wordpress articles | |
*/ | |
public class WordpressFragment extends Fragment { | |
private ListView feedListView = null; | |
private Activity mAct; | |
private WordpressGetTask mTask; | |
private WordpressGetTaskInfo mInfo; | |
private LinearLayout ll; | |
String perpage = "15"; | |
String apiurl; | |
String baseurl; | |
String searchurl; | |
String searchurlend; | |
String pageurl; | |
int orientation; | |
@SuppressLint("InflateParams") | |
@Override | |
public View onCreateView(LayoutInflater inflater, ViewGroup container, | |
Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
ll = (LinearLayout) inflater.inflate(R.layout.fragment_wordpress_list, | |
container, false); | |
setHasOptionsMenu(true); | |
apiurl = this.getArguments().getString(MainActivity.DATA); | |
constructUrls(); | |
/** Native Express Ad */ | |
orientation = getScreenOrientation(); | |
if (orientation==1) // 1 for Configuration.ORIENTATION_PORTRAIT | |
{ // 2 for Configuration.ORIENTATION_LANDSCAPE | |
Helper.admobLoader(ll.getContext(), getResources(), ll.findViewById(R.id.adView)); // 0 for Configuration.ORIENTATION_SQUARE | |
}else if(orientation == 2) | |
{ | |
Helper.admobLoader(ll.getContext(), getResources(), ll.findViewById(R.id.adViewLand)); | |
} | |
View footerView = inflater.inflate(R.layout.listview_footer, null); | |
RelativeLayout dialogLayout = (RelativeLayout) ll | |
.findViewById(R.id.progressBarHolder); | |
feedListView = (ListView) ll.findViewById(R.id.custom_list); | |
mInfo = new WordpressGetTaskInfo(footerView, feedListView, ll.getContext(), dialogLayout,ll, apiurl, false); | |
//----- | |
// feedListView.setOnClickListener(new View.OnClickListener() { | |
// @Override | |
// public void onClick(View v) | |
// { | |
// if(v!=null) | |
// Log.d("MyTag","id "+v.getId()); | |
// } | |
// }); | |
feedListView.setOnItemClickListener(new OnItemClickListener() { | |
@Override | |
public void onItemClick(AdapterView<?> a, View v, int position, | |
long id) { | |
try | |
{ | |
// final int pos=position; | |
// | |
// View thumbImage=v.findViewById(R.id.thumbImageContainer); | |
// if(thumbImage!=null) { | |
// thumbImage.setOnClickListener(new View.OnClickListener() { | |
// @Override | |
// public void onClick(View v) { | |
// try { | |
// Object o = feedListView.getItemAtPosition(pos); | |
// PostItem newsData = (PostItem) o; | |
// | |
// String youtube_link = WordpressDetailActivity.getYouTubeID(newsData.getContent()); | |
// if (youtube_link != "") { | |
// Intent intent = new Intent(mAct, YouTubePlayerActivity.class); | |
// intent.putExtra(YouTubePlayerActivity.EXTRA_VIDEO_ID, youtube_link); | |
// intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY); | |
// startActivity(intent); | |
// } | |
// } catch (Exception exp) { | |
// } | |
// } | |
// }); | |
// } | |
Object o = feedListView.getItemAtPosition(position); | |
PostItem newsData = (PostItem) o; | |
Intent intent = new Intent(mAct, WordpressDetailActivity.class); | |
intent.putExtra("feed", newsData); | |
intent.putExtra("apiurl", apiurl); | |
startActivity(intent); | |
} | |
catch(Exception exp) | |
{ | |
//Log.d("Excp", ""); | |
} | |
} | |
}); | |
feedListView.setOnScrollListener(new OnScrollListener() { | |
@Override | |
public void onScroll(AbsListView view, int firstVisibleItem, | |
int visibleItemCount, int totalItemCount) { | |
if (mInfo.feedListAdapter == null) | |
return; | |
if (mInfo.feedListAdapter.getCount() == 0) | |
return; | |
int l = visibleItemCount + firstVisibleItem; | |
if (l >= totalItemCount && !mInfo.isLoading && mInfo.curpage <= mInfo.pages) { | |
mTask = new WordpressGetTask(baseurl, false, mInfo); | |
mTask.execute(); | |
} | |
} | |
@Override | |
public void onScrollStateChanged(AbsListView view, int scrollState) { | |
} | |
}); | |
return ll; | |
} | |
@Override | |
public void onActivityCreated(Bundle savedInstanceState) { | |
super.onActivityCreated(savedInstanceState); | |
mAct = getActivity(); | |
mTask = new WordpressGetTask(baseurl, true, mInfo); | |
mTask.execute(); | |
} | |
public int getScreenOrientation() | |
{ | |
Display getOrient = getActivity().getWindowManager().getDefaultDisplay(); | |
int orientation = Configuration.ORIENTATION_UNDEFINED; | |
if(getOrient.getWidth()==getOrient.getHeight()){ | |
orientation = Configuration.ORIENTATION_SQUARE; | |
} else{ | |
if(getOrient.getWidth() < getOrient.getHeight()){ | |
orientation = Configuration.ORIENTATION_PORTRAIT; | |
}else { | |
orientation = Configuration.ORIENTATION_LANDSCAPE; | |
} | |
} | |
return orientation; | |
} | |
@Override | |
public void onConfigurationChanged(Configuration newConfig) { | |
super.onConfigurationChanged(newConfig); | |
if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) { | |
NativeExpressAdView adView = (NativeExpressAdView) ll.findViewById(R.id.adView); | |
LinearLayout parent= (LinearLayout) adView.getParent(); | |
parent.removeView(adView); | |
//adView.setVisibility(View.GONE); | |
Helper.admobLoader(ll.getContext(), getResources(), ll.findViewById(R.id.adViewLand)); | |
} | |
if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT) { | |
NativeExpressAdView adView = (NativeExpressAdView) ll.findViewById(R.id.adViewLand); | |
LinearLayout parent= (LinearLayout) adView.getParent(); | |
parent.removeView(adView); | |
//adView.setVisibility(View.GONE); | |
Helper.admobLoader(ll.getContext(), getResources(), ll.findViewById(R.id.adView)); | |
} | |
Log.d("ScrOre : ", "Screen Oriantation Changed"); | |
} | |
public void constructUrls() { | |
String[] parts = apiurl.split(","); | |
if (parts.length == 2) { | |
pageurl = parts[0] + "get_category_posts?category_slug=" + parts[1] | |
+ "&count=" + perpage + "&page="; | |
apiurl = parts[0]; | |
} else { | |
pageurl = apiurl | |
+ "get_recent_posts?exclude=comments,categories,custom_fields&count=" | |
+ perpage + "&page="; | |
} | |
baseurl = pageurl; | |
searchurl = apiurl + "get_search_results?count=" + perpage + "&search="; | |
searchurlend = "&page="; | |
} | |
@Override | |
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) { | |
inflater.inflate(R.menu.refresh_menu, menu); | |
// set & get the search button in the actionbar | |
final SearchView searchView = new SearchView(mAct); | |
searchView.setQueryHint(getResources().getString( | |
R.string.video_search_hint)); | |
searchView.setOnQueryTextListener(new OnQueryTextListener() { | |
// | |
@Override | |
public boolean onQueryTextSubmit(String query) { | |
try { | |
query = URLEncoder.encode(query, "UTF-8"); | |
} catch (UnsupportedEncodingException e) { | |
e.printStackTrace(); | |
} | |
searchView.clearFocus(); | |
baseurl = searchurl + query + searchurlend; | |
// if a task is active, cancel it | |
if (mTask.getStatus().equals(AsyncTask.Status.RUNNING)) { | |
mTask.cancel(true); | |
mTask = null; | |
if (mInfo.isLoading) { | |
mInfo.isLoading = false; | |
} | |
} | |
mTask = new WordpressGetTask(baseurl, true, mInfo); | |
mTask.execute(); | |
return true; | |
} | |
@Override | |
public boolean onQueryTextChange(String newText) { | |
return false; | |
} | |
}); | |
searchView | |
.addOnAttachStateChangeListener(new OnAttachStateChangeListener() { | |
@Override | |
public void onViewDetachedFromWindow(View arg0) { | |
if (!mInfo.isLoading) { | |
baseurl = pageurl; | |
mTask = new WordpressGetTask(baseurl, true, mInfo); | |
mTask.execute(); | |
} | |
} | |
@Override | |
public void onViewAttachedToWindow(View arg0) { | |
// search was opened | |
} | |
}); | |
// TODO make menu an xml item | |
menu.add("search") | |
.setIcon(R.drawable.ic_action_search) | |
.setActionView(searchView) | |
.setShowAsAction( | |
MenuItem.SHOW_AS_ACTION_IF_ROOM | |
| MenuItem.SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW); | |
} | |
@Override | |
public boolean onOptionsItemSelected(MenuItem item) { | |
switch (item.getItemId()) { | |
case R.id.refresh: | |
if (!mInfo.isLoading) { | |
baseurl = pageurl; | |
mTask = new WordpressGetTask(baseurl, true, mInfo); | |
mTask.execute(); | |
} else { | |
Toast.makeText(mAct, getString(R.string.already_loading), | |
Toast.LENGTH_LONG).show(); | |
} | |
default: | |
return super.onOptionsItemSelected(item); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment