Как найти два текстовых блока в одном элементе в RecyclerView? - PullRequest
1 голос
/ 17 июня 2019

Я уже создал RecyclerView с держателем представления и с одним списком.Но мне нужно один textView в начале и второй в конце.Как мне это сделать?

Я создал макет с двумя TextViews, там уже есть места.Но я должен установить для первого textView animalNames и для второго - peopleNames.Вот мой код:

MainActivity.java

public class MainActivity extends AppCompatActivity {
    MyRecyclerViewAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        ArrayList<String> animalNames = new ArrayList<>();
        animalNames.add("Horse");
        animalNames.add("Cow");
        animalNames.add("Camel");
        animalNames.add("Sheep");
        animalNames.add("Goat");


        ArrayList<String> peopleNames = new ArrayList<>();
        animalNames.add("Harry");
        animalNames.add("John");
        animalNames.add("Sergey");
        animalNames.add("Steve");
        animalNames.add("Woody");

        RecyclerView recyclerView = findViewById(R.id.rvAnimals);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        adapter = new MyRecyclerViewAdapter(this, animalNames);
        recyclerView.setAdapter(adapter);
        adapter.setClickListener(new MyRecyclerViewAdapter.ItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
                Toast.makeText(getApplicationContext(), "You clicked " + adapter.getItem(position) + " on row number " + position, Toast.LENGTH_SHORT).show();
            }
        });
    }
}

MyRecyclerViewAdapter.java

public class MyRecyclerViewAdapter extends RecyclerView.Adapter<MyRecyclerViewAdapter.ViewHolder> {
    private List<String> mData;
    private LayoutInflater mInflater;
    private ItemClickListener mClickListener;

    MyRecyclerViewAdapter(Context context, List<String> data) {
        this.mInflater = LayoutInflater.from(context);
        this.mData = data;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = mInflater.inflate(R.layout.custom_view, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        String animal = mData.get(position);
        holder.myTextView.setText(animal);
    }

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


    public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
        TextView myTextView;
        TextView myTextView2;

        ViewHolder(View itemView) {
            super(itemView);
            myTextView = itemView.findViewById(R.id.tvAnimalName);
            myTextView2 = itemView.findViewById(R.id.tvAnimalName2);
            itemView.setOnClickListener(this);
        }

        @Override
        public void onClick(View view) {
            if (mClickListener != null) mClickListener.onItemClick(view, getAdapterPosition());
        }
    }

    String getItem(int id) {
        return mData.get(id);
    }

    void setClickListener(ItemClickListener itemClickListener) {
        this.mClickListener = itemClickListener;
    }

    public interface ItemClickListener {
        void onItemClick(View view, int position);
    }
}

custom_view.xml

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

    <TextView
        android:id="@+id/tvAnimalName"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentStart="true"
        android:layout_marginStart="8dp"
        android:textSize="20sp" />


    <TextView
        android:id="@+id/tvPersonName"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentEnd="true"
        android:layout_marginEnd="8dp"
        android:textSize="20sp" />

</RelativeLayout>

activity_main.xml

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

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

</RelativeLayout>

1 Ответ

0 голосов
/ 17 июня 2019
    Pass the person array list also in the constructor of the recycler view

    //like in activity
    RecyclerView recyclerView = findViewById(R.id.rvAnimals);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        adapter = new MyRecyclerViewAdapter(this, animalNames,personNames);

    //in adapter class
    create a list for person also
    private List<String> mData;
    private List<String> mpersonData;

    // adapter constructor
    MyRecyclerViewAdapter(Context context, List<String> data,List<String> mpersonData) {
        this.mInflater = LayoutInflater.from(context);
        this.mData = data;
    this.mpersonData = mpersonData;
    }

    //onbind view holder

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        String animal = mData.get(position);
        String person = mData.get(position);
        holder.myTextView.setText(animal);
    holder.myTextView.setText(person);
    }

    //change the tv id name to person
      ViewHolder(View itemView) {
            super(itemView);
            myTextView = itemView.findViewById(R.id.tvAnimalName);
            // just change this to person name
            myTextView2 = itemView.findViewById(R.id.tvPersonalName);
            itemView.setOnClickListener(this);
        }

    Now you will set person name also in the recycler view text field


Paste this code instead of relative layout use linear layout

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

        <TextView
            android:id="@+id/tvAnimalName"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:weight="1"
            android:gravity="start"
            android:layout_marginStart="8dp"
            android:textSize="20sp" />


        <TextView
            android:id="@+id/tvPersonName"
             android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:weight="1"
            android:gravity="end"
            android:layout_marginEnd="8dp"
            android:textSize="20sp" />

    </LinearLayout>

and you can have two columns as mentioned in the question 
...