我使用Recycler View显示倒计时器列表,每个计时器都显示在卡片视图中。但是,在向下滚动Recycler View并向上滚动后,卡片的某些内容会发生变化,如下面的两个屏幕截图所示。滚动后更改红色内容。
我在主活动中使用JSON对象请求加载数据,如下所示:
public class MainActivity extends AppCompatActivity
{
String showUrl = "http://192.168.56.1/GetData.php";
private List<information> info;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
info = new ArrayList<>();
setContentView(R.layout.activity_main); //Cardlayout code
createJSON();
RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recycler_view);
recyclerView.setHasFixedSize(true);
LinearLayoutManager layout = new LinearLayoutManager(this);
recyclerView.setLayoutManager(layout);
myadapter adapter = new myadapter(info);
recyclerView.setAdapter(adapter);
}
private void createJSON()
{
RequestQueue requestQueue = Singelton.instantinate().getRequestqueue();
JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.POST, showUrl,
new Response.Listener<JSONObject>()
{
@Override
public void onResponse(JSONObject response)
{
parseJSONResponse(response);
}
},
new Response.ErrorListener()
{....});
requestQueue.add(jsonObjectRequest);
}
private void parseJSONResponse(JSONObject response)
{
try
{
JSONArray jsonArray = response.getJSONArray("students");
for (int i = 0; i <= jsonArray.length(); i++)
{
JSONObject jsonObject = jsonArray.getJSONObject(i);
int a = jsonObject.getInt("Year");
int b = jsonObject.getInt("Month");
int c = jsonObject.getInt("Day");
int d = jsonObject.getInt("Hours");
int e = jsonObject.getInt("Minutes");
String mid = jsonObject.getString("Movieid");
int sesa=jsonObject.getInt("ses");
info.add(new information(a, b - 1, c, d, e,mid,sesa));
}
} catch (JSONException e)
{
e.printStackTrace();
}
}
我的回收站视图适配器如下
public class myadapter extends RecyclerView.Adapter<myadapter.myviewholder>
{
public static int SECONDS_IN_A_DAY = 24 * 60 * 60;
List<information> info;
Singelton singelton = Singelton.instantinate();
ImageLoader imageLoader = singelton.getImageLoader();
String x;
myadapter(List<information> info)
{
this.info = info;
}
@Override
public myviewholder onCreateViewHolder(ViewGroup parent, int viewType)
{
View v = LayoutInflater.from(parent.getContext())
.inflate(R.layout.cardcontent, parent, false);
myviewholder pvh = new myviewholder(v);
return pvh;
}
@Override
public void onBindViewHolder(final myviewholder holder, final int position)
{
information in = info.get(position);
holder.to = in;
Calendar targetdate = Calendar.getInstance(); //start of timer code
targetdate.set(in.a, in.b, in.c, in.d, in.e);
Calendar currentDate = Calendar.getInstance();
final long diff = targetdate.getTimeInMillis() - currentDate.getTimeInMillis();
new CountDownTimer(diff, 1)
{ //will change every 1ms i.e 0.001 seconds
public void onTick(long millisUntilFinished)
{
long diffSec = millisUntilFinished / 1000;
long days = diffSec / SECONDS_IN_A_DAY;
long secondsDay = diffSec % SECONDS_IN_A_DAY;
long seconds = secondsDay % 60;
long minutes = (secondsDay / 60) % 60;
long hours = (secondsDay / 3600);
long countmilliforsec = millisUntilFinished % 1000;
long milliseconds = countmilliforsec % (SECONDS_IN_A_DAY);
holder.textView1.setText(days + " days " + hours + " hours " + minutes + " minutes \n" + seconds + " secs " + String.format("%03d", milliseconds) + " millisconds " + "remaining ");
holder.textView3.setText("Position" + position); //Dispalying the position here
}
public void onFinish()
{
holder.textView1.setText("done!");
}
}.start(); //end of timer code
}
public class myviewholder extends RecyclerView.ViewHolder{...}
我做错了什么。我知道每次向上滚动时都会调用onBindViewHolder,但为什么我收到了错误的位置呢?
答案 0 :(得分:-1)
不是为每个视图创建计时器,而是尝试创建一个计时器,通过调用RecyclerView
来刷新整个notifyDataSetChanged()
。通过这种方式,您可以保持同步。