Last active
July 4, 2017 11:32
-
-
Save sabadow/a7dd2575325b676a113e to your computer and use it in GitHub Desktop.
Fabric time line custom adapter to overrides onClick event
This file contains 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" | |
android:orientation="vertical" | |
android:layout_width="match_parent" | |
android:layout_height="match_parent"> | |
<TextView | |
android:id="@id/android:empty" | |
android:layout_width="match_parent" | |
android:layout_height="match_parent" | |
android:gravity="center_horizontal|center_vertical" | |
android:text="Loading tweets"/> | |
<ListView | |
android:id="@id/android:list" | |
android:layout_width="match_parent" | |
android:layout_height="0dp" | |
android:layout_weight="1" | |
android:divider="#434543" | |
android:dividerHeight="1dp" | |
android:drawSelectorOnTop="false"/> | |
</LinearLayout> |
This file contains 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
import android.app.ListActivity; | |
import android.content.Context; | |
import android.os.Bundle; | |
import android.view.View; | |
import android.view.ViewGroup; | |
import android.widget.Toast; | |
import com.twitter.sdk.android.Twitter; | |
import com.twitter.sdk.android.core.TwitterAuthConfig; | |
import com.twitter.sdk.android.core.models.Tweet; | |
import com.twitter.sdk.android.tweetui.Timeline; | |
import com.twitter.sdk.android.tweetui.TweetTimelineListAdapter; | |
import com.twitter.sdk.android.tweetui.UserTimeline; | |
import io.fabric.sdk.android.Fabric; | |
public class MainActivity extends ListActivity { | |
// Note: Your consumer key and secret should be obfuscated in your source code before shipping. | |
private static final String TWITTER_KEY = ""; | |
private static final String TWITTER_SECRET = ""; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
TwitterAuthConfig authConfig = new TwitterAuthConfig(TWITTER_KEY, TWITTER_SECRET); | |
Fabric.with(this, new Twitter(authConfig)); | |
setContentView(R.layout.activity_main); | |
final UserTimeline userTimeline = new UserTimeline.Builder().screenName("fabric").build(); | |
final CustomTweetTimelineListAdapter adapter = new CustomTweetTimelineListAdapter(this, userTimeline); | |
setListAdapter(adapter); | |
} | |
/** | |
* Custom Adapter to overrides view onClickListener | |
*/ | |
class CustomTweetTimelineListAdapter extends TweetTimelineListAdapter { | |
public CustomTweetTimelineListAdapter(Context context, Timeline<Tweet> timeline) { | |
super(context, timeline); | |
} | |
@Override | |
public View getView(final int position, View convertView, ViewGroup parent) { | |
View view = super.getView(position, convertView, parent); | |
//disable subviews | |
if(view instanceof ViewGroup){ | |
disableViewAndSubViews((ViewGroup) view); | |
} | |
//enable root view and attach custom listener | |
view.setEnabled(true); | |
view.setOnClickListener(new View.OnClickListener() { | |
@Override | |
public void onClick(View v) { | |
String tweetId = "click tweetId:"+getItemId(position); | |
Toast.makeText(context, tweetId, Toast.LENGTH_SHORT).show(); | |
} | |
}); | |
return view; | |
} | |
private void disableViewAndSubViews(ViewGroup layout) { | |
layout.setEnabled(false); | |
for (int i = 0; i < layout.getChildCount(); i++) { | |
View child = layout.getChildAt(i); | |
if (child instanceof ViewGroup) { | |
disableViewAndSubViews((ViewGroup) child); | |
} else { | |
child.setEnabled(false); | |
child.setClickable(false); | |
child.setLongClickable(false); | |
} | |
} | |
} | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment