gpt4 book ai didi

java - 用于多个 recyclerview 的单个适配器

转载 作者:太空狗 更新时间:2023-10-29 14:41:43 25 4
gpt4 key购买 nike

我想为共享相同布局设计的多个 RecyclerView 使用单个适配器。我创建了一个自定义类,其中包括用于文本、图片和音频的 3 ArrayList,并且我在自定义类中插入来自不同 fragment 的资源。

适配器类:

package com.example.adil.tabpen;

import android.media.MediaPlayer;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.adil.tabpen.Fragment.TwoFragment;
import com.example.adil.tabpen.OurData;

/**
* Created by Adil on 10/2/2017.
*/

public class ListAdapter extends RecyclerView.Adapter {
MediaPlayer mediaPlayer;
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType)
{
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_item, parent, false);
return new ListViewHolder(view);
}

@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position)
{
((ListViewHolder) holder).bindView(position);
}

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

private class ListViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener
{
private TextView text;
private ImageView image;
private ImageButton button;

public ListViewHolder(View itemview)
{
super(itemview);
text = (TextView) itemview.findViewById(R.id.textF);
image = (ImageView) itemview.findViewById(R.id.imageF);
button = (ImageButton) itemview.findViewById(R.id.button);
itemview.setOnClickListener(this);
}

public void bindView(final int position)
{
text.setText(OurData.title.get(position));
image.setImageResource(OurData.pic.get(position));
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v)
{
mediaPlayer = MediaPlayer.create(v.getContext(), OurData.record.get(position));
mediaPlayer.start();
}
});
}

public void onClick (View view)
{

}
}
}

第一个 fragment (小写字母):

package com.example.adil.tabpen.Fragment;

import android.app.FragmentTransaction;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.ListFragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;

import com.example.adil.tabpen.Adapter;
import com.example.adil.tabpen.ListAdapter;
import com.example.adil.tabpen.OurData;
import com.example.adil.tabpen.R;

import java.util.ArrayList;

/**
* A simple {@link Fragment} subclass.
*/
public class OneFragment extends Fragment
{
public OneFragment() {
// Required empty public constructor
}
ListView listView;
ArrayList<String> players;
ArrayList<Integer> images;

//String [] players = {"Tamim","Shakib","Mashrafe","Ashraful"};
//int [] images = {R.drawable.a,R.drawable.b,R.drawable.c,R.drawable.a};

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_one, container, false);

//Built-in adapter
/*String[] menuItem = {"Hello","Hi","Bye"};
ListView listView = (ListView) view.findViewById(R.id.listView);
ArrayAdapter<String> listViewAdapter = new ArrayAdapter<String>(
getActivity(),
android.R.layout.simple_list_item_1,
menuItem
);
listView.setAdapter(listViewAdapter);*/

//Custom adapter
/*listView = (ListView) view.findViewById(R.id.listView);
players = new ArrayList<String>();
images = new ArrayList<Integer>();

players.add("Ashraful");
images.add(R.drawable.a);
players.add("Aftab");
images.add(R.drawable.a);
players.add("Tamim");
images.add(R.drawable.a);
Adapter adapter = new Adapter(getActivity(),players,images);
listView.setAdapter(adapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener()
{
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

Toast.makeText(getActivity(),players.get(position), Toast.LENGTH_SHORT).show();
}
});*/
//Using recycleview
OurData ourData = new OurData();
ourData.pic.add(R.drawable.aa);
ourData.title.add("Ant");
ourData.record.add(R.raw.a);

ourData.pic.add(R.drawable.bb);
ourData.title.add("Bat");
ourData.record.add(R.raw.a);

ourData.pic.add(R.drawable.cc);
ourData.title.add("Cry");
ourData.record.add(R.raw.a);

ourData.pic.add(R.drawable.dd);
ourData.record.add(R.raw.a);
ourData.title.add("Dish");

ourData.pic.add(R.drawable.ee);
ourData.title.add("Elephant");
ourData.record.add(R.raw.a);

ourData.pic.add(R.drawable.ff);
ourData.title.add("Fish");
ourData.record.add(R.raw.a);

ourData.pic.add(R.drawable.gg);
ourData.record.add(R.raw.a);
ourData.title.add("Goose");

ourData.pic.add(R.drawable.hh);
ourData.title.add("Hen");
ourData.record.add(R.raw.a);

ourData.pic.add(R.drawable.i);
ourData.title.add("Ice-cream");
ourData.record.add(R.raw.a);

ourData.pic.add(R.drawable.jj);
ourData.title.add("Jeep");
ourData.record.add(R.raw.a);

ourData.pic.add(R.drawable.kk);
ourData.title.add("Kite");
ourData.record.add(R.raw.a);

ourData.pic.add(R.drawable.mm);
ourData.title.add("Mother");
ourData.record.add(R.raw.a);

ourData.pic.add(R.drawable.nn);
ourData.title.add("Nut");
ourData.record.add(R.raw.a);

ourData.pic.add(R.drawable.oo);
ourData.record.add(R.raw.a);
ourData.title.add("Orange");

ourData.pic.add(R.drawable.pp);
ourData.title.add("Price");
ourData.record.add(R.raw.a);

ourData.pic.add(R.drawable.qq);
ourData.title.add("Quality");
ourData.record.add(R.raw.a);

ourData.pic.add(R.drawable.rr);
ourData.title.add("Rock");
ourData.record.add(R.raw.a);

ourData.pic.add(R.drawable.ss);
ourData.title.add("Six");
ourData.record.add(R.raw.a);

ourData.pic.add(R.drawable.tt);
ourData.title.add("Time");
ourData.record.add(R.raw.a);

ourData.pic.add(R.drawable.uu);
ourData.title.add("Use");
ourData.record.add(R.raw.a);

ourData.pic.add(R.drawable.vv);
ourData.record.add(R.raw.a);
ourData.title.add("Via");

ourData.pic.add(R.drawable.ww);
ourData.title.add("Watch");
ourData.record.add(R.raw.a);

ourData.pic.add(R.drawable.xx);
ourData.title.add("Xylophone");
ourData.record.add(R.raw.a);

ourData.pic.add(R.drawable.yy);
ourData.title.add("Yolk");
ourData.record.add(R.raw.a);

ourData.pic.add(R.drawable.zz);
ourData.title.add("Zebra");
ourData.record.add(R.raw.a);

RecyclerView recyclerView = (RecyclerView) view.findViewById(R.id.listRecyclerView);
ListAdapter listAdapter = new ListAdapter();
recyclerView.setAdapter(listAdapter);
RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(getActivity());
recyclerView.setLayoutManager(layoutManager);
return view;
}

/* private void onViewCreated()
{

}*/
}

第二个 fragment (大写字母):

package com.example.adil.tabpen.Fragment;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.example.adil.tabpen.ListAdapter;
import com.example.adil.tabpen.OurData;
import com.example.adil.tabpen.R;

/**
* A simple {@link Fragment} subclass.
*/
public class TwoFragment extends Fragment {
public TwoFragment() {
// Required empty public constructor
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_two, container, false);

OurData ourData = new OurData();
ourData.pic.add(R.drawable.a);
ourData.title.add("Apple");
ourData.record.add(R.raw.a);

ourData.pic.add(R.drawable.b);
ourData.title.add("Ball");
ourData.record.add(R.raw.a);

ourData.pic.add(R.drawable.c);
ourData.title.add("Cat");
ourData.record.add(R.raw.a);

ourData.pic.add(R.drawable.d);
ourData.record.add(R.raw.a);
ourData.title.add("Dog");

ourData.pic.add(R.drawable.e);
ourData.title.add("Egg");
ourData.record.add(R.raw.a);

ourData.pic.add(R.drawable.f);
ourData.title.add("Flower");
ourData.record.add(R.raw.a);

ourData.pic.add(R.drawable.g);
ourData.record.add(R.raw.a);
ourData.title.add("Game");

ourData.pic.add(R.drawable.h);
ourData.title.add("Horse");
ourData.record.add(R.raw.a);

ourData.pic.add(R.drawable.i);
ourData.title.add("Igloo");
ourData.record.add(R.raw.a);

ourData.pic.add(R.drawable.j);
ourData.title.add("Jacket");
ourData.record.add(R.raw.a);

ourData.pic.add(R.drawable.k);
ourData.title.add("Key");
ourData.record.add(R.raw.a);

ourData.pic.add(R.drawable.m);
ourData.title.add("Mango");
ourData.record.add(R.raw.a);

ourData.pic.add(R.drawable.n);
ourData.title.add("Net");
ourData.record.add(R.raw.a);

ourData.pic.add(R.drawable.o);
ourData.record.add(R.raw.a);
ourData.title.add("Owl");

ourData.pic.add(R.drawable.p);
ourData.title.add("Potato");
ourData.record.add(R.raw.a);

ourData.pic.add(R.drawable.q);
ourData.title.add("Quit");
ourData.record.add(R.raw.a);

ourData.pic.add(R.drawable.r);
ourData.title.add("Race");
ourData.record.add(R.raw.a);

ourData.pic.add(R.drawable.s);
ourData.title.add("Shop");
ourData.record.add(R.raw.a);

ourData.pic.add(R.drawable.t);
ourData.title.add("Tomato");
ourData.record.add(R.raw.a);

ourData.pic.add(R.drawable.u);
ourData.title.add("Umbrella");
ourData.record.add(R.raw.a);

ourData.pic.add(R.drawable.v);
ourData.record.add(R.raw.a);
ourData.title.add("Vehicle");

ourData.pic.add(R.drawable.w);
ourData.title.add("Window");
ourData.record.add(R.raw.a);

ourData.pic.add(R.drawable.x);
ourData.title.add("X-Ray");
ourData.record.add(R.raw.a);

ourData.pic.add(R.drawable.y);
ourData.title.add("Yellow");
ourData.record.add(R.raw.a);

ourData.pic.add(R.drawable.z);
ourData.title.add("Zoo");
ourData.record.add(R.raw.a);

RecyclerView recyclerView = (RecyclerView) view.findViewById(R.id.listRecyclerView);
ListAdapter listAdapter = new ListAdapter();
recyclerView.setAdapter((listAdapter));
RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(getActivity());
recyclerView.setLayoutManager(layoutManager);

return view;
}
}

第一个 fragment 包括小写字母、音频和图片。第二个 fragment 包括大写字母、音频和图片。我的代码工作正常,但我想我搞砸了。当我运行代码时,我在两个 fragment 中都得到了大写字母和小写字母。但我的目标是在大写字母 fragment 中包含大写字母,在小写字母 fragment 中包含小写字母。我找不到应该在哪里更改。

最佳答案

试试这个

package com.example.adil.tabpen;

import android.media.MediaPlayer;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.adil.tabpen.Fragment.TwoFragment;
import com.example.adil.tabpen.OurData;

/**
* Created by Adil on 10/2/2017.
*/

public class ListAdapter extends RecyclerView.Adapter {
MediaPlayer mediaPlayer;
private boolean isUpperCase;
public ListAdapter(boolean isUpperCase){
this.isUpperCase = isUpperCase;
}
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType)
{
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_item, parent, false);
return new ListViewHolder(view);
}

@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position)
{
((ListViewHolder) holder).bindView(position);

}

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

private class ListViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener
{
private TextView text;
private ImageView image;
private ImageButton button;

public ListViewHolder(View itemview)
{
super(itemview);
text = (TextView) itemview.findViewById(R.id.textF);
image = (ImageView) itemview.findViewById(R.id.imageF);
button = (ImageButton) itemview.findViewById(R.id.button);
itemview.setOnClickListener(this);
}


public void bindView(final int position)
{
text.setText(isUpperCase ? OurData.title.get(position).toUpperCase() : OurData.title.get(position).toLowerCase());
image.setImageResource(OurData.pic.get(position));
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v)
{

mediaPlayer = MediaPlayer.create(v.getContext(), OurData.record.get(position));
mediaPlayer.start();
}
});
}

public void onClick (View view)
{

}


}
}

然后在创建适配器时,您应该要么

ListAdapter listAdapter = new ListAdapter(true); 仅获取大写字母或ListAdapter listAdapter = new ListAdapter(false); 用于小写字母

关于java - 用于多个 recyclerview 的单个适配器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46992573/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com