【问题标题】:Custom ListView with image, text and add item button带有图像、文本和添加项目按钮的自定义 ListView
【发布时间】:2017-06-10 18:23:50
【问题描述】:

我对 Android 还很陌生,无法做到这一点,整天都在搜索。

Layout I'm trying to create.

我已经创建了自定义 xml 布局,我找到了在创建时添加项目的方法,但我需要列表为空,而不是按下按钮从列表中添加。

这是布局:

<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">

<TableRow>

<ImageView
android:id="@+id/info_img_view"
android:layout_width="30dp"
        android:layout_height="30dp" />

    <TextView
        android:id="@+id/info_text_view"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center" />

    <TextView
        android:id="@+id/info_time_text_view"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:gravity="right"
        />

</TableRow>

我在主要活动中有一个 ListView:

<ListView
            android:id="@+id/list_view"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />

我该怎么做?如果有人能指出我可以了解代码实际在做什么的资源,我将不胜感激,而不是我找到的教程,我只是复制和粘贴......

谢谢!

编辑,对我想要实现的目标进行更多解释

我有 6 个按钮。当按下按钮时,它应该添加一个包含两个文本视图的列表项,以及总共三个图像中的一个图像。

例如,如果按下 Button1:添加列表项 > "Text one" "Text one" "imageTwoOfThree"。

然后,如果按下 Button2:在顶部添加列表项 >“文本二”“文本二”“imageTwoOfThree”

等等...文本是硬编码的。

【问题讨论】:

  • 你能发布你的java类吗
  • 不要使用表格布局,而是使用线性布局,这样会很容易。对于您正在寻找的功能,只需在该按钮单击上设置您的 Listview 适配器。既然你不想要代码所以试试吧
  • @quicklearner 我在 java 类中什么都没有...
  • @sumit 我可以这样做,我需要代码,但我认为如果有人能至少解释一下会很好......
  • 看看它,这是一个很好的教程,详细解释了所有内容:androidhive.info/2016/01/android-working-with-recycler-view 如果你卡在某个地方,请告诉我

标签: java android listview arraylist


【解决方案1】:

这里使用这个: 我创建了一个带有 dummydata 的列表,您可以根据自己更改文本和图像

首先创建一个Data类:

public class Data {

    private String name,price;
    private int imageId;
    public Data(){}

    public Data(String name,String price,int imageId){
        this.name = name;
        this.price = price;
        this.imageId = imageId;
    }

    public String getPrice() {
        return price;
    }

    public void setPrice(String price) {
        this.price = price;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getImageId() {
        return imageId;
    }

    public void setImageId(int imageId) {
        this.imageId = imageId;
    }
}

然后创建一个 ListView Adapter 来处理你的数据:

public class ListViewAdaptor extends RecyclerView.Adapter<ListViewAdaptor.MyViewHolder> {
    private List<Data> mDataList;

    public class MyViewHolder extends RecyclerView.ViewHolder{
        public TextView name,price;
        public ImageView imageView;

        public MyViewHolder(View view){
            super(view);
            name = (TextView) view.findViewById(R.id.name);
            price= (TextView) view.findViewById(R.id.price);
            imageView = (ImageView) view.findViewById(R.id.image);
        }
    }

    public ListViewAdaptor(List<Data> dataList){
        this.mDataList = dataList;
    }


    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.list_view_item, parent, false);

        return new MyViewHolder(itemView);
    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, int position) {
        Data data = mDataList.get(position);
        holder.name.setText(data.getName());
        holder.price.setText(data.getPrice());
        holder.imageView.setImageResource(data.getImageId());
    }

    @Override
    public int getItemCount() {
        return mDataList.size();
    }
}

列表视图项目的布局将其命名为 list_view_item:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@mipmap/ic_launcher"
        android:id="@+id/image"
        />

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/name"
        android:text="name"
        android:gravity="center"
        android:textSize="26sp"
        android:layout_weight="1"/>

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/price"
        android:text="price"
        android:gravity="center"
        android:textSize="26sp"
        android:layout_weight="1"/>

</LinearLayout>

然后从您要添加 listView 的活动中在布局中添加 recyclerView:

<android.support.v7.widget.RecyclerView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/recycler_view">

    </android.support.v7.widget.RecyclerView>

然后像这样使用这个rec​​yclerview: //我已经从我的 MainActivity 中调用了它,你可以在任何你喜欢的活动中使用它

public class MainActivity extends AppCompatActivity {


    private RecyclerView mRecyclerView;
    private ListViewAdaptor mAdapter;
    private List<Data> mDataList = new ArrayList<>();

    private static final String TAG = "MainActivity";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mRecyclerView = (RecyclerView) findViewById(R.id.recycler_view);

        mAdapter = new ListViewAdaptor(mDataList);
        RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(getApplicationContext());
        mRecyclerView.setLayoutManager(mLayoutManager);
        mRecyclerView.setItemAnimator(new DefaultItemAnimator());
        mRecyclerView.setHasFixedSize(true);
        mRecyclerView.setAdapter(mAdapter);
        prepareList();
    }

    public void prepareList(){
       Data data = new Data("Item1","Price1",R.drawable.star);
        mDataList.add(data);
        data = new Data("Item2","Price2",R.drawable.star);
        mDataList.add(data);
        data = new Data("Item3","Price3",R.drawable.star);
        mDataList.add(data);
        data = new Data("Item4","Price4",R.drawable.star);
        mDataList.add(data);
        data = new Data("Item5","Price5",R.drawable.star);
        mDataList.add(data);
    }

}

希望对你有帮助!!!

【讨论】:

  • 绝对精彩!我做了一些重大的改变,但现在我确切地知道我做错了什么。谢谢一百万!
【解决方案2】:

您会找到很多相关的在线资源。但是,让我简要介绍一下。 假设您想在一行中存储 2 个文本视图。 1. 对于行的每个布局,您需要制作一个自定义布局 xml 文件并在那里设计您的布局。

  1. 接下来,创建一个类来存储数据并通过getter返回数据。

    class Category {
    private String categoryName;
    private String categoryImageURL;
    
    Category (String categoryName, String categoryImageUrl) {
        this.categoryName = categoryName;
        this.categoryImageURL = categoryImageUrl;
    }
    String getCategoryName () {
        return categoryName;
    }
    String getCategoryImageURL () {
        return categoryImageURL;
    }
    }
    
  2. 现在制作一个自定义数组适配器,它将链接数据和布局。 用上面定义的类扩展 arrayadapter 类。

    private class categoryArrayAdapter extends ArrayAdapter<Category> {
    private Context context;
    private List<Category> categories;
    
    public categoryArrayAdapter (Context context, int resource, ArrayList<Category> objects) {
        super(context, resource, objects);
    
        this.context = context;
        this.categories = objects;
    }
    
    public View getView (int position, View convertView, ViewGroup parent) {
        Category category = categories.get(position);
    
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
        View           view     = inflater.inflate(R.layout.category_row_view, null);
    
        TextView  categoryListRowText  = (TextView) view.findViewById(R.id.categoryListRowText);
        TextView  categoryListRowImage    = (TextView) view.findViewById(R.id.categoryListRowImage);
    
        categoryListRowText.setText(category.getCategoryName());
        categoryListRowImage.setText(category.getCategoryImageURL());
    
        return view;
    }
    }
    
  3. 最后将适配器链接到您的列表视图

    ArrayAdapter<Category> adapter = new categoryArrayAdapter(this, 0, categories);
    categoryListView.setAdapter(adapter);
    

希望这回答了你的问题。

【讨论】:

    猜你喜欢
    • 2010-12-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-17
    • 2011-01-14
    • 1970-01-01
    相关资源
    最近更新 更多