我正在尝试使用列表视图和表格视图开发facebook类型的Feed。每行都有列表视图中包含注释的Feed帖子。我将feed的数据对象存储在数组列表中。
class FeedPost{
String username;
String imageUrl;
String feedpostText;
ArrayList<Comment> comments;}
在我的视图中,我正在这样做
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)));
}
}
再次在addcomments函数中,我正在对注释布局进行膨胀,以便可以将其添加到表格布局中
因此,要在列表视图中显示单个Feed,我会检查每个Feed项以查看注释并在Feed中显示它们。这真的很痛苦而且很慢。
我尝试使用静态支架进行布局,而不是每次都给它们充气。再说那也没有用。如果有多个不同的视图,那么我会尝试commonsguy的“SackOfViewsAdapter”。但实际上并没有不同的观点。
我认为facebook使用webview并呈现html以显示用户的提要。使用android原生布局和代码做同样的事情的最佳方法是什么。你能帮我建议用来展示这种复杂数据的最佳方法吗?
答案 0 :(得分:1)
这是我的实施。我删除了很多视图,只想展示如何回收远程图像视图(来自Ignition / Droid-fu)。
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;
}
答案 1 :(得分:0)
两件事:重新使用视图并制作Asynctask来加载Feed(将Feed和for移动到任务中)。这样你就不会扼杀UI线程。
我命令重新使用它的视图很容易。像这样:
if (convertView == null) {
row = (LinearLayout) LayoutInflater.from(this.context).inflate(R.layout.account, parent, false);
} else {
row = (LinearLayout) convertView;
}
对于asynctask,我会将您想要更新的视图传递给asynctask并让任务在完成后更新视图。