【问题标题】:I know how to populate a list with custom items via custom adapter. But how do I populate it with a few different types of items?我知道如何通过自定义适配器使用自定义项填充列表。但是如何用几种不同类型的项目填充它?
【发布时间】:2013-12-29 13:15:44
【问题描述】:

情况:

我有一个 android 应用程序,用户在其中执行某些操作,这些操作应该显示在像 Facebook 的提要这样的提要中。操作有 4 种类型 - 成为朋友、关注某人、评论和评分。

我知道该怎么做(参见下面的示例*)

我知道如何扩展 BaseAdapter 并自定义它的 getView 方法,这样我就可以创建一个自定义列表项,以后可以使用该列表项填充列表视图。示例 - 用户的朋友列表,其中包含他们的头像、他们的姓名和每个人的几个按钮。

我不知道该怎么做,我想请你帮忙

我不知道如何使提要针对最近发生的 10 个动作显示不同的自定义布局 - 这个人与那个人成为朋友,然后这个人评价了那个人的工作,然后这个人评论了那个东西......

我该如何实现?

*一个创建自定义可适配项,然后用它们填充列表视图的示例:

public class MyProfileFriendAdapter extends BaseAdapter {

    private ArrayList<String> userNames =  new ArrayList<String>();
    private ArrayList<String> userAvatarPaths   = new ArrayList<String>();
    private ArrayList<Integer> userIds = new ArrayList<Integer>();
    private Context context;
    private LayoutInflater layoutInflater;

    private ImageButton iMyFriendsFriendAvatarImage;
    private TextView tvMyFriendsListFriendName;
    private Button bMyFriendsMessage;
    private Button bMyFriendsUnfriend;

    public MyProfileFriendAdapter(Context context, ArrayList<String> userNames, ArrayList<String> userAvatarPaths,  ArrayList<Integer> userIds){

        this.context = context;
        this.userNames = userNames;
        this.userAvatarPaths = userAvatarPaths;
        this.userIds = userIds;

        layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    @Override
    public int getCount() {
        return userIds.size();
    }

    @Override
    public Object getItem(int arg0) {
        return userIds.get(arg0)    ;
    }

    @Override
    public long getItemId(int position) {
        return userIds.get(position);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if(convertView==null) {
            convertView = layoutInflater.inflate(R.layout.item_adapterable_my_profile_friend, parent, false);
        }

        iMyFriendsFriendAvatarImage = (ImageButton) convertView.findViewById(R.id.iMyFriendsFriendAvatarImage);
        tvMyFriendsListFriendName = (TextView) convertView.findViewById(R.id.tvMyFriendsListFriendName);

        bMyFriendsMessage = (Button) convertView.findViewById(R.id.bMyFriendsMessage);
        bMyFriendsUnfriend = (Button) convertView.findViewById(R.id.bMyFriendsUnfriend);    

        tvMyFriendsListFriendName.setText(userNames.get(position));

        iMyFriendsFriendAvatarImage.setImageBitmap(BitmapFactory.decodeFile(userAvatarPaths.get(position)));

        return convertView;
    }

}

然后在包含列表视图的活动中,我执行以下操作:

    listOfFriends = (ListView) findViewById(R.id.lvMyProfileFriendsHolder);

    MyProfileFriendAdapter myProfileFriendAdapter = new MyProfileFriendAdapter(this, userNames, userAvatarPaths,  userIds);
    listOfFriends.setAdapter(myProfileFriendAdapter);

那么,当我需要根据用户在我的应用中执行的最后操作,在同一个列表中使用各种不同的可适配项时,我该怎么办?

【问题讨论】:

标签: android listview


【解决方案1】:

看看这个:

http://www.jiahaoliuliu.com/2012/08/android-list-view-with-different-views.html

这是一个简单的例子。 欲了解更多信息,您可以查找getViewCount & getItemViewType

【讨论】:

  • 太好了,我可以在 8 分钟内接受。 BaseAdapter 类中有这些方法吗?还是在活动中?
  • getViewCount, getItemViewType(int) 属于BaseAdapter。
猜你喜欢
  • 1970-01-01
  • 2019-08-22
  • 2011-05-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-08-21
相关资源
最近更新 更多