【问题标题】:Creating list inside the Activity after clicking RecyclerView单击 RecyclerView 后在 Activity 内创建列表
【发布时间】:2017-08-11 04:31:16
【问题描述】:

正如标题所说,我想在单击RecyclerView 后在 Activity 中创建一个新列表。我正在使用JSON 来获取数据,其中的数据显然有一个名称、cmets 和日期时间的列表。我可以将它们设置为TextView,但这不是我想要的。我需要将它们显示为列表。该列表应包含名称、cmets 和日期时间。谢谢!

【问题讨论】:

  • 你需要json解析并显示到RecyclerView。
  • 我已经使用适配器绑定数据并将其显示给recyclerview。我可以使用 get 检索值并将其显示到视图中,但数据将彼此相邻。所以我想为此创建一个列表
  • 在此处显示您的代码。
  • 您需要将自定义适配器与您想要的布局一起使用。
  • 请在此处添加您的代码

标签: android data-binding android-recyclerview


【解决方案1】:

您需要使用自定义适配器而不是 ArrayAdapter。这样您就可以随心所欲地自定义视图。

例如你的情况

row_item.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:padding="10dp">

    <!-- Name -->

    <TextView
        android:id="@+id/name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:text="Marshmallow"
        android:textAppearance="?android:attr/textAppearanceSmall"
        android:textColor="@android:color/black" />

    <!-- Comments-->

    <TextView
        android:id="@+id/comments"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/name"
        android:layout_marginTop="5dp"
        android:text="Android 6.0"
        android:textColor="@android:color/black" />


    <!-- Date and Time-->

  <TextView
        android:id="@+id/dateandtime"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/comments"
        android:layout_marginTop="5dp"
        android:text="11th August"
        android:textColor="@android:color/black" />


</RelativeLayout>

DataModel.java

public class DataModel {

    String name;
    String comment,
    String date;


    public DataModel(String name, String type, String comment, String date) {
        this.name=name;
        this.comment=comment;
        this.date=date;

    }

    public String getName() {
        return name;
    }

    public String getComment() {
        return comment;
    }

    public String getDate() {
        return date;
    }

}

最后你的自定义适配器应该如下所示

CustomAdapter.java

public class CustomAdapter extends ArrayAdapter<DataModel> implements View.OnClickListener{

    private ArrayList<DataModel> dataSet;
    Context mContext;

    // View lookup cache
    private static class ViewHolder {
        TextView txtName;
        TextView txtComment;
        TextView txtDate;
    }

    public CustomAdapter(ArrayList<DataModel> data, Context context) {
        super(context, R.layout.row_item, data);
        this.dataSet = data;
        this.mContext=context;

    }

    @Override
    public void onClick(View v) {

       // your on click code
    }

    private int lastPosition = -1;

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // Get the data item for this position
        DataModel dataModel = getItem(position);
        // Check if an existing view is being reused, otherwise inflate the view
        ViewHolder viewHolder; // view lookup cache stored in tag

        final View result;

        if (convertView == null) {

            viewHolder = new ViewHolder();
            LayoutInflater inflater = LayoutInflater.from(getContext());
            convertView = inflater.inflate(R.layout.row_item, parent, false);
            viewHolder.txtName = (TextView) convertView.findViewById(R.id.name);
            viewHolder.txtComment = (TextView) convertView.findViewById(R.id.comments);
            viewHolder.txtDate = (TextView) convertView.findViewById(R.id.dateandtime);


            result=convertView;

            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
            result=convertView;
        }


        viewHolder.txtName.setText(dataModel.getName());
        viewHolder.txtComment.setText(dataModel.getComment());
        viewHolder.txtDate.setText(dataModel.getDate());
        viewHolder.info.setOnClickListener(this);
        viewHolder.info.setTag(position);
        // Return the completed view to render on screen
        return convertView;
    }
}

你必须像这样设置适配器。

ArrayList<DataModel> dataModels; // then add your data into it

CustomAdapter  adapter= new CustomAdapter(dataModels,getApplicationContext());

listView.setAdapter(adapter);

查看example 以获得更好的想法。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-02-02
    • 1970-01-01
    • 1970-01-01
    • 2019-09-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多