您好我正在使用循环器视图根据显示视图的值从json检索图像和文本。如果数据小于10日期,则所有工作正常 在回收者视图中只有10个图像显示一次。在我向下滚动并向上滚动图像后,图像消失了。不知道我错在哪里。
这是我的代码
jsondata
"DATA":[
{
"ID":"82",
"MSG":"",
"IMAGE":"http://14.18.19.15/data/chat_img/20160308102624439533297.jpg",
"TYPE":"1",
"STATUS":"0",
"DATE":"2016-03-08 10:26:24"
},
{
"ID":"81",
"MSG":"saggergergergaskfasjdfahsdfalshdfaldfafdjkahsdfkjahlfdkjahfls",
"IMAGE":"",
"TYPE":"1",
"STATUS":"0",
"DATE":"2016-03-08 10:25:40"
},
{
"ID":"80",
"MSG":"Hi",
"IMAGE":"",
"TYPE":"2",
"STATUS":"0",
"DATE":"2016-03-07 13:24:00"
},....
]
Activity code
recyclerView = (RecyclerView) findViewById(R.id.my_recycler_view);
context=this;
messege = new ArrayList<String>();
date = new ArrayList<String>();
image = new ArrayList<String>();
getjsondata();
//adapter = new RecyclerAdapter(context, stockArr);
//recyclerView.setAdapter(adapter);
// Toast.makeText(getApplicationContext(),""+stockArr.length,Toast.LENGTH_SHORT).show();
System.out.print("ARRRRRRRRRRRRRRRRRRRRRRY"+stockArr);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
}
public void getjsondata()
{
StringRequest stringRequest = new StringRequest(Request.Method.POST, REGISTER_URL,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
// Toast.makeText(MainActivity.this, response, Toast.LENGTH_LONG).show();
try {
JSONObject homeobj = new JSONObject(response);
JSONObject insteller = homeobj.getJSONObject("INSTELLAR");
status = insteller.getString("STATUS");
if(status.equals("success")) {
JSONArray data = insteller.getJSONArray("DATA");
for(int i=0; i < data.length(); i++) {
JSONObject jsonObject = data.getJSONObject(i);
msg = jsonObject.getString("MSG").toString();
img =jsonObject.getString("IMAGE").toString();
dte =jsonObject.getString("DATE").toString();
messege.add(msg);
date.add(dte);
image.add(img);
}
stockArr = new String[messege.size()];
stockArr = messege.toArray(stockArr);
stockArr1 = new String[date.size()];
stockArr1 = date.toArray(stockArr1);
stockArr2 = new String[image.size()];
stockArr2 = image.toArray(stockArr2);
/* for(String s : stockArr)
System.out.println(s);*/
Toast.makeText(getApplicationContext(),"TOAST SIZE"+messege.size(), Toast.LENGTH_SHORT).show();
adapter = new RecyclerAdapter(context, stockArr,stockArr1,stockArr2);
recyclerView.setAdapter(adapter);
}
else
{
Toast.makeText(MainActivity.this, "Failed", Toast.LENGTH_LONG).show();
}
} catch (JSONException e) {
e.printStackTrace();
Toast.makeText(MainActivity.this, "Error in Json", Toast.LENGTH_LONG).show();
}
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(MainActivity.this, error.toString(), Toast.LENGTH_LONG).show();
}
}) {
@Override
protected Map<String, String> getParams() {
Map<String, String> params = new HashMap<String, String>();
params.put("clientid", KEY_ClientID);
params.put("adminid", KEY_ADMINID);
params.put("userid", KEY_USERID);
return params;
}
};
RequestQueue requestQueue = Volley.newRequestQueue(this);
requestQueue.add(stringRequest);
}
}
Adapter.java
public class RecyclerAdapter extends
RecyclerView.Adapter<RecyclerViewHolder> {
String name[];
String image[];
String date[];
// String [] date={"date1","date2","date3","date4"};
Context context;
LayoutInflater inflater;
public RecyclerAdapter(Context context, String[] message,String[] datee,String[] imageee) {
this.context=context;
inflater=LayoutInflater.from(context);
name=message;
date=datee;
image=imageee;
}
@Override
public RecyclerViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v=inflater.inflate(R.layout.item_list, parent, false);
RecyclerViewHolder viewHolder=new RecyclerViewHolder(v);
return viewHolder;
}
@Override
public void onBindViewHolder(RecyclerViewHolder holder, int position)
{
try
{
if(name[position].equals("")) {
for(int i = 0;i<image.length;i++) {
Picasso.with(context).load(image[position]).into(holder.imageView);
holder.imageView.setTag(holder);
holder.tv2.setText(date[position]);
}
}
if(image[position].equals(""))
{
holder.imageView.setVisibility(View.GONE);
holder.tv1.setText(name[position]);
holder.tv2.setText(date[position]);
}
// holder.tv1.setVisibility(View.GONE);
} catch (Exception e) {
e.printStackTrace();
// holder.tv1.setVisibility(View.GONE);
// holder.imageView.setVisibility(View.GONE);
// holder.imageView.setTag(holder);
}
}
@Override
public int getItemCount() {
return name.length;
}
}
答案 0 :(得分:1)
RecyclerView重用行视图。因此,一旦设置为消失,您的ImageView可见性永远不可见。只需在if(image[position].equals(""))
添加其他内容,如下所示
else{
holder.imageView.setVisibility(View.Visible);
}
让我知道这是否有效。
答案 1 :(得分:0)
如果您更改了视图状态,请确保每个视图状态都匹配,因为转换视图已记住视图状态。
if(flag){
view.changeState();
}else{
view.changeState();
}