我有两个RecyclerViews。 Parent RecyclerView工作正常,但是孩子的onBindViewHolder无法正常工作。它不会在滚动时执行,因此我的文本框没有按预期更新。
有没有人可以帮助我在每个卷轴上执行子回收物物RecyclerView的onBindViewHolder?
以下是更好理解的代码。
MainActivity.java
RecyclerView recyclerView;
HistoryMainAdapter adapter;
conn = new ConnectionClass();
int pid=1,month=05,year=2016;
datalistMain = conn.getHistoryDetails(pid,month,year);
adapter=new HistoryMainAdapter(datalistMain,dateString,count,getApplicationContext());
RecyclerView.LayoutManager layoutManager=new LinearLayoutManager(getApplicationContext());
recyclerView.setLayoutManager(layoutManager);
recyclerView.setItemAnimator(new DefaultItemAnimator());
recyclerView.setAdapter(adapter);
HistoryMainAdapter.java(父)
public class HistoryMainAdapter extends RecyclerView.Adapter<HistoryMainAdapter.MyViewHolder> {
private LayoutInflater inflater;
MyViewHolder holder;
Context contextmain;
ArrayList<tblExamHistory> datalistMain = new ArrayList<>();
ArrayList<tblExamHistory> datalistSub = new ArrayList<>();
ArrayList<String> dateString=new ArrayList<>();
RecyclerView recyclerView;
HistorySubAdapter adapter;
String date,prevday,day;
int count,cnt=0,k=0;
public HistoryMainAdapter(ArrayList<tblExamHistory> datalistMain,ArrayList<String> dateString, int count, Context context)
{
inflater=LayoutInflater.from(context);
this.datalistMain=datalistMain;
this.count=count;
this.dateString=dateString;
contextmain=context;
date=datalistMain.get(0).getAttendedDate();
day = date.substring(Math.max(date.length() - 2, 0)); //To get last two digits(day) of date
prevday="00";
}
@Override
public HistoryMainAdapter.MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = inflater.inflate(R.layout.history_main, parent, false);
holder = new MyViewHolder(view);
return holder;
}
@Override
public void onBindViewHolder(MyViewHolder holder, int position) {
int c=1;
holder.txtView.setText(dateString.get(position));
}
@Override
public int getItemCount() {
return count;
}
public class MyViewHolder extends RecyclerView.ViewHolder {
TextView txtView;
public MyViewHolder(View itemView) {
super(itemView);
txtView=(TextView)itemView.findViewById(R.id.txtDate);
tblExamHistory data = new tblExamHistory();
int init=0;
addRecyclers(init,itemView,data);
}
}
public void addValues(int x,View itemView,tblExamHistory data)
{
data.setAttendedDate(datalistMain.get(x).getAttendedDate());
data.setExamTitle(datalistMain.get(x).get_ExamTitle());
data.setStartTime(datalistMain.get(x).getStartTime());
datalistSub.add(data);
recyclerView = (RecyclerView) itemView.findViewById(R.id.recyclerView1);
RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(contextmain);
recyclerView.setLayoutManager(layoutManager);
recyclerView.setItemAnimator(new DefaultItemAnimator());
adapter = new HistorySubAdapter(datalistSub, contextmain);
recyclerView.setAdapter(adapter);
}
HistorySubAdapter.java(子)
public class HistorySubAdapter extends RecyclerView.Adapter<HistorySubAdapter.MyViewHolder> {
MyViewHolder holder;
LayoutInflater inflater;
ArrayList<tblExamHistory> datalist;
public HistorySubAdapter(ArrayList<tblExamHistory> datalist, Context context)
{
inflater= LayoutInflater.from(context);
this.datalist=datalist;
}
@Override
public HistorySubAdapter.MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = inflater.inflate(R.layout.history_sub, parent, false);
holder = new MyViewHolder(view);
return holder;
}
@Override
public void onBindViewHolder(HistorySubAdapter.MyViewHolder holder, int position) {
> //this triggers only on initialisation
1231
> holder.txtTime.setText(datalist.get(position).getStartTime());
> holder.txtTitle.setText(datalist.get(position).get_ExamTitle());
}
@Override
public int getItemCount() {
return datalist.size();
}
public class MyViewHolder extends RecyclerView.ViewHolder {
TextView txtTime,txtTitle;
public MyViewHolder(View itemView) {
super(itemView);
txtTime=(TextView) itemView.findViewById(R.id.txtTime);
txtTitle=(TextView) itemView.findViewById(R.id.txtExamName);
}
}
}
屏幕截图: - Screenshot 1 Screenshot 2
答案 0 :(得分:0)
替换了HistoryMainAdapter.Java中的以下代码段。因为我忘记添加一行代码,即; public class MyViewHolder extends RecyclerView.ViewHolder {
TextView txtView;
public MyViewHolder(View itemView) {
super(itemView);
txtView=(TextView)itemView.findViewById(R.id.txtDate);
tblExamHistory data = new tblExamHistory();
int init=0;
addRecyclers(init,itemView,data);
recyclerView = (RecyclerView) itemView.findViewById(R.id.recyclerView1);
RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(contextmain);
recyclerView.setLayoutManager(layoutManager);
recyclerView.setItemAnimator(new DefaultItemAnimator());
adapter = new HistorySubAdapter(datalistSub, contextmain);
recyclerView.setAdapter(adapter);
}
}
public void addValues(int x)
{
tblExamHistory data= new tblExamHistory();
data.setAttendedDate(datalistMain.get(x).getAttendedDate());
data.setExamTitle(datalistMain.get(x).get_ExamTitle());
data.setStartTime(datalistMain.get(x).getStartTime());
datalistSub.add(data);
}
这就是为什么recyleview为所有行获得相同的值。现在它工作正常。
public class AsyncHttpClient {
public AsyncHttpClient() {
}
public static class SocketIORequest {
private String mUri;
private String mEndpoint;
private List<BasicNameValuePair> mHeaders;
public SocketIORequest(String uri) {
this(uri, null);
}
public SocketIORequest(String uri, String endpoint) {
this(uri, endpoint, null);
}
public SocketIORequest(String uri, String endpoint, List<BasicNameValuePair> headers) {
mUri = Uri.parse(uri).buildUpon().encodedPath("/socket.io/1/").build().toString();
mEndpoint = endpoint;
mHeaders = headers;
}
public String getUri() {
return mUri;
}
public String getEndpoint() {
return mEndpoint;
}
public List<BasicNameValuePair> getHeaders() {
return mHeaders;
}
}
public static interface StringCallback {
public void onCompleted(final Exception e, String result);
}
public static interface WebSocketConnectCallback {
public void onCompleted(Exception ex, WebSocketClient webSocket);
}
public void executeString(final SocketIORequest socketIORequest, final StringCallback stringCallback) {
new AsyncTask<Void, Void, Void>() {
@Override
protected Void doInBackground(Void... params) {
AndroidHttpClient httpClient = AndroidHttpClient.newInstance("android-websockets-2.0");
HttpPost post = new HttpPost(socketIORequest.getUri());
addHeadersToRequest(post, socketIORequest.getHeaders());
try {
HttpResponse res = httpClient.execute(post);
String responseString = readToEnd(res.getEntity().getContent());
if (stringCallback != null) {
stringCallback.onCompleted(null, responseString);
}
} catch (IOException e) {
if (stringCallback != null) {
stringCallback.onCompleted(e, null);
}
} finally {
httpClient.close();
httpClient = null;
}
return null;
}
private void addHeadersToRequest(HttpRequest request, List<BasicNameValuePair> headers) {
if (headers != null) {
Iterator<BasicNameValuePair> it = headers.iterator();
while (it.hasNext()) {
BasicNameValuePair header = it.next();
request.addHeader(header.getName(), header.getValue());
}
}
}
}.execute();
}
private byte[] readToEndAsArray(InputStream input) throws IOException {
DataInputStream dis = new DataInputStream(input);
byte[] stuff = new byte[1024];
ByteArrayOutputStream buff = new ByteArrayOutputStream();
int read = 0;
while ((read = dis.read(stuff)) != -1) {
buff.write(stuff, 0, read);
}
return buff.toByteArray();
}
private String readToEnd(InputStream input) throws IOException {
return new String(readToEndAsArray(input));
}