gpt4 book ai didi

android - Activity 中的 FragmentPagerAdapter 中的 RecyclerView

转载 作者:行者123 更新时间:2023-11-29 22:42:00 26 4
gpt4 key购买 nike

当我到达我的 Activity 时,我调用 FragmentPagerAdapter 来处理我的不同选项卡。在我的一个选项卡中,我想显示一个 RecyclerView,但他从未出现过,有了断点,我看到应用程序处理了所有事情。所以我不知道去哪里找。

这是我的 Activity :

public class InfoClient extends AppCompatActivity implements Response.ErrorListener, Response.Listener<String> {
TextView tv_client = null;
RequestQueue queue;
String url;
StringRequest stringRequest;
ViewPager viewPager;
TabLayout tabLayout;
ViewPagerAdapter viewPagerAdapter;

String nomClient;
String adresseClient;
String codePostalClient;
String villeClient;
String telClient;





private Toolbar toolbar;


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_info_client);
viewPager=findViewById(R.id.viewPager);
Bundle extras = getIntent().getExtras();
String leclient = (String) extras.get("leClient");
queue = Volley.newRequestQueue(InfoClient.this);
url = "http://myip:80/select/infoClient/"+leclient;
stringRequest = new StringRequest(GET, url, InfoClient.this, InfoClient.this);
queue.add(stringRequest);

}


@Override
public void onErrorResponse(VolleyError error) {

}


@Override
public void onResponse(String response) {


try{


JSONObject client = new JSONObject(response);

String idClient = client.get("CLCLEUNIK").toString();
String nomClient = client.get("NOM").toString();
String adresseClient = client.get("ADRESSE").toString();
String codePostalClient = client.get("CODEP").toString();
String villeClient = client.get("VILLE").toString();
String telClient = client.get("TEL").toString();




viewPagerAdapter = new ViewPagerAdapter(getSupportFragmentManager());
viewPagerAdapter.addClient(idClient,nomClient,adresseClient,codePostalClient,villeClient,telClient);
viewPager.setAdapter(viewPagerAdapter);

tabLayout=findViewById(R.id.tabs);


tabLayout.setupWithViewPager(viewPager);




}catch (JSONException e) {
e.printStackTrace();
}

}
}

这是我的 FragmentPagerAdapter :

public class ViewPagerAdapter extends FragmentPagerAdapter {

RequestQueue queue;
private String idClient;
private String nomClient;
private String adresseClient;
private String codePostalClient;
private String villeClient;
private String telClient;

private String[] libelle = { "Coordonnées", "Contacts", "TI" };

public void addClient (String p_idClient,String p_nomclient, String p_adresseClient, String p_codePostalClient, String p_villeClient, String p_telClient) {
idClient=p_idClient;
nomClient=p_nomclient;
adresseClient=p_adresseClient;
codePostalClient=p_codePostalClient;
villeClient=p_villeClient;
telClient=p_telClient;

}

ViewPagerAdapter(@NonNull FragmentManager fm) {
super(fm);
}

@NonNull
@Override
public Fragment getItem(int position) {

switch (position){
case 0:


Coordonne_client coordonneClient = new Coordonne_client();

Bundle bundle = new Bundle();
bundle.putString("nomCli", nomClient);
bundle.putString("adresseCli", adresseClient);
bundle.putString("codePostalClient", codePostalClient);
bundle.putString("villeClient", villeClient);
bundle.putString("telClient", telClient);
coordonneClient.setArguments(bundle);

return coordonneClient;

case 1:
Contacts_client contacts_client = new Contacts_client();
Bundle bundle2 = new Bundle();
bundle2.putString("idClient",idClient);
contacts_client.setArguments(bundle2);

return contacts_client;



default:

return null;


}


}



@Override
public int getCount(){

return 2;

}

@Nullable
@Override
public CharSequence getPageTitle(int position) {

return libelle[position];
}
}

这是我要放置 Recycler View 的 fragment :

public class Contacts_client extends Fragment implements Response.ErrorListener, Response.Listener<String> {

private TextView textView;
private String idClient;

RequestQueue queue;
String url;
StringRequest stringRequest;

private RecyclerView recyclerView;
private RecyclerView.Adapter mAdapter;
private RecyclerView.LayoutManager layoutManager;
MyAdapterContactsClient myAdapterContactsClient =new MyAdapterContactsClient();



public Contacts_client() {

// 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_contacts_client, container, false);
idClient = getArguments().get("idClient").toString();

queue = Volley.newRequestQueue(getActivity().getApplicationContext());
url = "http://myip/search/contacts/"+idClient;
stringRequest = new StringRequest(GET, url, Contacts_client.this, Contacts_client.this);
queue.add(stringRequest);











return view;
}

@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {

RecyclerView recyclerView = (RecyclerView)getView().findViewById(R.id.recyclerview_contact_cli);

recyclerView.setHasFixedSize(true);
layoutManager= new LinearLayoutManager(getActivity().getApplicationContext());
recyclerView.setAdapter(myAdapterContactsClient);

}

@Override
public void onErrorResponse(VolleyError error) {

}

@Override
public void onResponse(String response) {
RecyclerView recyclerView = (RecyclerView)getView().findViewById(R.id.recyclerview_contact_cli);
Gson gsonContactClient = new Gson();
ResponseJson responseJson = new ResponseJson();
responseJson = (ResponseJson) gsonContactClient.fromJson(response, ResponseJson.class);
List<Object> listContactsClient = responseJson.getResponse();
try {
JSONArray contacts = new JSONArray(listContactsClient.toArray());
ArrayList<String> nomsCli = new ArrayList<String>();
ArrayList<String> prenomsCli = new ArrayList<String>();
ArrayList<String> telsCli = new ArrayList<String>();


for(int i=0; i<contacts.length(); i++){
JSONObject unContact = contacts.getJSONObject(i);
nomsCli.add(unContact.get("NOM").toString());
prenomsCli.add(unContact.get("PRENOM").toString());
telsCli.add(unContact.get("TEL").toString());
}

myAdapterContactsClient.getList(nomsCli, prenomsCli, telsCli);
recyclerView.setAdapter(myAdapterContactsClient);





} catch (JSONException e) {
e.printStackTrace();
}

}
}

这个 fragment 的.xml:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".Fragments_info_client.Contacts_client">

<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recyclerview_contact_cli"
android:layout_width="409dp"
android:layout_height="729dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>

至少,适配器:

public class MyAdapterContactsClient extends RecyclerView.Adapter<MyAdapterContactsClient.MyViewHolder> {

private List<String> nomContacts = new ArrayList<>();
private List<String> prenomContacts = new ArrayList<>();
private List<String> telContacts = new ArrayList<>();


public void getList(ArrayList<String> nomContact, ArrayList<String> prenomContact, ArrayList<String> telContact){

nomContacts.clear();
prenomContacts.clear();
telContacts.clear();

for(int i =1; i<nomContact.size();i++){
nomContacts.add(nomContact.get(i));
prenomContacts.add(prenomContact.get(i));
telContacts.add(telContact.get(i));
}
this.notifyDataSetChanged();
}

public String getNomContact(int position){
return nomContacts.get(position);
}

public String getPrenomContact(int position){
return prenomContacts.get(position);
}

public String getTelContact(int position){
return telContacts.get(position);
}





@NonNull
@Override
public MyAdapterContactsClient.MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
LayoutInflater inflater = LayoutInflater.from(parent.getContext());
View view = inflater.inflate(R.layout.list_cell_contacts_client, parent, false);
return new MyViewHolder(view);
}

@Override
public void onBindViewHolder(@NonNull MyAdapterContactsClient.MyViewHolder holder, int position) {

String nom = nomContacts.get(position);
String prenom = prenomContacts.get(position);
String tel = telContacts.get(position);

holder.display(nom, prenom, tel);


}

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

public class MyViewHolder extends RecyclerView.ViewHolder{

private TextView nomCli;
private TextView prenomCli;
private TextView telCli;


public MyViewHolder(@NonNull View itemView) {
super(itemView);

nomCli=(TextView)itemView.findViewById(R.id.tv_contacts_nomcli);
prenomCli=itemView.findViewById(R.id.tv_contacts_prenomcli);
telCli=itemView.findViewById(R.id.tv_contacts_telcli);



}

public void display(String nom, String prenom, String tel) {
nomCli.setText(nom);
prenomCli.setText(prenom);
telCli.setText(tel);

}
}
}

我知道代码太多,但我真的不知道去哪里找。让我们一起寻找 ;) 谢谢


更新

断点告诉我,我们从未处理过 recyclerview 的适配器,只有 getList() 方法和脚本在我到达 Activity 时执行,而不是在选项卡中执行。

最佳答案

你忘记将布局管理器设置为 RecyclerView

 recyclerView.setHasFixedSize(true);
layoutManager= new LinearLayoutManager(getActivity().getApplicationContext());
recyclerView.setLayoutManager(layoutManager);
recyclerView.setAdapter(myAdapterContactsClient);

关于android - Activity 中的 FragmentPagerAdapter 中的 RecyclerView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58956661/

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