所以我有一个卡片视图的recyclerview列表。我正在尝试在用户点击卡片时更新每个视图中的文本时间文本,然后在时间过去后重新设置。我在recyclerviewadapter中设置了一个内部方法,但是当我调用updateTimes(从片段调用)时,它不会重置文本。有人可以告诉我我做错了吗?
def get_patterns_defaults(string,
re_digits=re.compile("(\d+)"),
re_alpha=re.compile("(?i)([A-Z]+)")
):
"""
Takes a string and returns found groups
of numeric and alphabetic characters.
"""
digits = re_digits.findall(string)
alpha = re_alpha.findall(string)
return digits, alpha
from timeit import Timer
test_string = "99 bottles of beer on the wall"
t = Timer(lambda: get_patterns(test_string))
t2 = Timer(lambda: get_patterns_defaults(test_string))
print t.timeit(number=100000) # compiled in function body
print t2.timeit(number=100000) # compiled in args
0.629958152771
0.474529981613
片段代码
import java.util.ArrayList;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.net.Uri;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.view.LayoutInflater;
import java.util.Calendar;
import android.content.Intent;
import java.text.SimpleDateFormat;
import java.util.Locale;
public class RecyclerAdapter extends RecyclerView.Adapter<FaucetHolder> {
private ArrayList<Faucet> faucets;
private Context context;
private SharedPreferences sharedPref;
private String dateFormat = "h:mm a";
public RecyclerAdapter(ArrayList<Faucet> faucetsI, Context context) {
this.faucets = faucetsI;
this.context = context;
}
@Override
public FaucetHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.card_view, viewGroup, false);
FaucetHolder f = new FaucetHolder(v);
return f;
}
@Override
public void onBindViewHolder(FaucetHolder f, int k) {
final Faucet faucet = faucets.get(k);
f.titleText.setOnClickListener(new View.OnClickListener() {
public void onClick(View btn) {
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(faucet.getLink()));
setRenewal(faucet);
notifyDataSetChanged();
context.startActivity(intent);
}
});
f.titleText.setText(faucet.getName());
sharedPref = context.getSharedPreferences("myPref", 0);
String time = sharedPref.getString(faucet.getSPName(), "could not retrieve time");
Log.e("tree", time);
f.timeText.setText(time);
}
@Override
public int getItemCount() {
return faucets.size();
}
private void setRenewal(Faucet g) {
sharedPref = context.getSharedPreferences("myPref", 0);
SharedPreferences.Editor edit = sharedPref.edit();
Calendar c = Calendar.getInstance();
long current = c.getTimeInMillis();
String x = sharedPref.getString(g.getSPName(), null);
long future = current + g.getLength();
g.setCT(future);
c.setTimeInMillis(future);
SimpleDateFormat df = new SimpleDateFormat(dateFormat, Locale.US);
String date = df.format(c.getTime());
edit.putString(g.getSPName(), date).commit();
}
public void updateTimes() {
sharedPref = context.getSharedPreferences("myPref", 0);
SharedPreferences.Editor edit = sharedPref.edit();
for ( Faucet f : faucets ) {
boolean m = checkifPast(f);
if ( m == true ) {
edit.putString(f.getSPName(), "Ready!").commit();
}
}
notifyDataSetChanged();
}
private boolean checkifPast(Faucet f) {
Calendar c = Calendar.getInstance();
long comp = c.getTimeInMillis();
if ( comp > f.getCT() ) {
return false;
}
return true;
}
}
答案 0 :(得分:4)
notifyDataSetChanged
应该用作最后的手段,因为它没有指定发生了什么,因此适配器不再有效,这导致列表不仅重新绑定,而且还重新布局所有可见视图。相反,您应该改为使用notifyItem[Range][Inserted|Changed|Removed]()
方法之一,notifyItemChanged(int position)会这样做。
在updateText()
中编辑文字,但不要将结果存储在任何字段中。然后,将调用bindViewHolder()
,调用setText(getTime())
并将文本重置为其先前的值。这可能就是为什么没有任何变化您应该向ViewHolder添加一个方法,该方法首先存储结果,然后调用TextView.setText()。
如果您的onClick()
从未被调用,您可能需要检查是否已将视图设置为可对焦且可在其布局文件中单击。
这是我在StackOverflow上的第一个awnser,英语不是我的第一语言,所以如果我输错了,请原谅。
答案 1 :(得分:0)
两件事,
updateText
被叫到哪里。 notifyDataSetChanged()
来更新适配器,让适配器重新绘制列表。 我添加了一个简单的回收站视图,并为textView添加了一个监听器。它应该在您调用文本更新后工作,然后调用notifyDataSetChanged()
,请参阅下面的
public class MyRecyclerAdapter extends RecyclerView.Adapter<FeedListRowHolder> {
private List<FeedItem> feedItemList;
private Context mContext;
public MyRecyclerAdapter(Context context, List<FeedItem> feedItemList) {
this.feedItemList = feedItemList;
this.mContext = context;
}
@Override
public FeedListRowHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.list_row, null);
FeedListRowHolder mh = new FeedListRowHolder(v);
return mh;
}
@Override
public void onBindViewHolder(FeedListRowHolder feedListRowHolder, int i) {
final FeedItem feedItem = feedItemList.get(i);
feedListRowHolder.title.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
updateText(feedItem);
notifyDataSetChanged();
}
});
Picasso.with(mContext).load(feedItem.getThumbnail())
.error(R.drawable.placeholder)
.placeholder(R.drawable.placeholder)
.into(feedListRowHolder.thumbnail);
feedListRowHolder.title.setText(Html.fromHtml(feedItem.getTitle()));
}
@Override
public int getItemCount()
{
return (null != feedItemList ? feedItemList.size() : 0);
}
private void updateText(FeedItem feedItem)
{
feedItem.setTitle("TEST!");
}
}