Reputation: 157
I am trying to develop a facebook kind of feeds using list view and table view. Each row has the feed post with comments in the list view. I am storing the data objects of feed in the array list.
class FeedPost{
String username;
String imageUrl;
String feedpostText;
ArrayList<Comment> comments;}
In my get view i am doing like this
public View getView(final int position, View convertView, ViewGroup parent) {
feedItem = FeedItems.get(position);
convertView = layoutInflater.inflate(R.layout.feed_main, null);
TableLayout mainFeedLayout = (TableLayout) convertView
.findViewById(R.id.blogFeedTableLayout);
TextView displayName = (TextView) convertView
.findViewById(R.id.feed_username);
TextView description = (TextView) convertView
.findViewById(R.id.comment_text);
// Set the text to the texview
.
.
.
// Adding comments to the feed
for (int i = 0; i < feedItem.feedComments.size(); i++) {
mainFeedLayout .addView(addComment(
convertView, feedItem.feedComments.get(i)));
}
}
Again in addcomments functions i am inflating the comment layout so that it can be added to the table layout
So for a single feed to be displayed in the list view, I am checking the each feed item for comments and displaying them in the feed. This is really painful and slow.
I tried using the static holders for layouts instead of inflating them each time. Again that didnt work also. If there are mulitple different views, then i would have tried "SackOfViewsAdapter " of commonsguy. But actually it is not different views.
I think facebook uses the webview and render the html for the showing the feeds of the user. What is the best way to do the same using the android native layout and code. Can you help me in suggesting the best method to use for showing this kind of complex data?
Upvotes: 1
Views: 1256
Reputation: 1094
This is my implementation. I removed a lot of views, just wanted to show how a remote image view (from Ignition / Droid-fu) could be recycled.
public class DealAdapter extends ArrayAdapter<DealObject> {
private Context mContext;
private Activity mActivity;
private ArrayList<DealObject> mItems;
private int mXmlId;
public DealAdapter(Context context, int textViewResourceId, ArrayList<DealObject> items, Activity activity) {
super(context, textViewResourceId, items);
this.mContext = context.getApplicationContext();
this.mActivity = activity;
this.mItems = items;
this.mXmlId = textViewResourceId;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
//View v = null;
View v = convertView;
ViewHolder holder = null;
RemoteImageLoader imageLoader = new RemoteImageLoader(mContext, true);
RemoteImageView dealImage = null;
DealObject mo = mItems.get(position);
try {
if (v == null) {
LayoutInflater vi = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(mXmlId, null);
holder = new ViewHolder();
dealImage = (RemoteImageView) v.findViewById(R.id.deal_image);
holder.dealImage = dealImage;
v.setTag(holder);
} else {
// Get the ViewHolder back to get fast access to the ImageView.
holder = (ViewHolder) convertView.getTag();
holder.dealImage.setBackgroundColor(Color.WHITE);
dealImage = holder.dealImage;
}
if(mo.getImage() != null){
// calling reset is important to prevent old images from displaying in a recycled view.
dealImage.reset();
holder.dealImage.setImageUrl(imageUrl);
try {
holder.dealImage.loadImage();
}
catch (Exception ex) {
}
}
}
catch (Exception ex) {
ex.printStackTrace();
}
return v;
}
private static final class ViewHolder {
private RemoteImageView dealImage;
}
Upvotes: 1
Reputation: 9978
Two things: Re use views and make an Asynctask to load the feed (Move the feed and the for to the task). That way you won't choke the UI thread.
I order to re use the views its easy. Something like this:
if (convertView == null) {
row = (LinearLayout) LayoutInflater.from(this.context).inflate(R.layout.account, parent, false);
} else {
row = (LinearLayout) convertView;
}
For the asynctask, I would pass the view that you want to update to the asynctask and let the task update the view when is done.
Upvotes: 0