列表视图中 itemview 的表格布局 android 性能

发布于 2024-12-23 16:25:16 字数 1374 浏览 2 评论 0原文

我正在尝试使用列表视图和表格视图开发 Facebook 类型的提要。每行在列表视图中都有带有评论的提要帖子。我将 feed 的数据对象存储在数组列表中。

class FeedPost{
   String username;
   String imageUrl;
   String feedpostText;
   ArrayList<Comment> comments;}

在我的 get 视图中,我这样做

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 函数中,我正在膨胀评论布局,以便可以将其添加到表布局中

因此,对于要在列表视图中显示的单个提要,我正在检查每个提要项目评论并在提要中显示它们。这确实是痛苦且缓慢的。

我尝试使用静态持有者进行布局,而不是每次都膨胀它们。同样,这也不起作用。如果有多个不同的视图,那么我会尝试 commonsguy 的“SackOfViewsAdapter”。但实际上这并不是不同的观点。

我认为 facebook 使用 webview 并渲染 html 来显示用户的提要。使用 android 本机布局和代码执行相同操作的最佳方法是什么?您能帮我建议显示此类复杂数据的最佳方法吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

别想她 2024-12-30 16:25:16

这是我的实现。我删除了很多视图,只是想展示如何回收远程图像视图(来自 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;
}

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;
}
晨曦÷微暖 2024-12-30 16:25:16

两件事:重新使用视图并创建一个异步任务来加载提要(将提要和 for 移至任务)。这样你就不会阻塞 UI 线程。

我命令重新使用视图很容易。像这样的事情:

if (convertView == null) {
            row = (LinearLayout) LayoutInflater.from(this.context).inflate(R.layout.account, parent, false);
            } else {
            row = (LinearLayout) convertView;
            }

对于 asynctask,我会将您想要更新的视图传递给 asynctask,并让任务在完成时更新视图。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文