我想用粗体将新添加的项目标记到我的列表中。一旦用户看到这个项目(即它在屏幕上显示 3 秒),我想将其标记为已读(即删除粗体)。我知道如何用粗体标记一些项目。但是当用户看到该项目时如何捕捉事件呢?
这个问题非常接近我正在寻找的内容。但它没有公认的答案。并且不太清楚如何等待 3 秒。
我想用粗体将新添加的项目标记到我的列表中。一旦用户看到这个项目(即它在屏幕上显示 3 秒),我想将其标记为已读(即删除粗体)。我知道如何用粗体标记一些项目。但是当用户看到该项目时如何捕捉事件呢?
这个问题非常接近我正在寻找的内容。但它没有公认的答案。并且不太清楚如何等待 3 秒。
Hints:
Your adapter has a getView().
Your adapter has a data source (a list, content provider, etc. of some sort).
Your adapter knows which position(s) are visible (because the list asked for those).
Combined with an OnScrollListener, you can tell when the list is moving.
If you use a view holder pattern (and you should) in your adapter, you can tell when views are being reused.
You can also set a 3 sec timer that will "flag" the items as read, as long as they remain visible.
You can tell if an item is visible by asking the list (getFirstVisiblePosition() and the last counterpart).
That's a starting point.
在您的 getView 方法上启动一个线程。
- 检查是否已读取项目。
- 如果没有,请将标识符设置为视图的标记。
- 开始你的话题。
- 后为前。4 秒睡眠。检查该视图标签是否仍然等于标识符。
- 如果是,请更改视图样式。并将项目设置为在您的数据结构中读取。
email.xml(项目布局):
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/background"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/title"
android:layout_width="match_parent"
android:layout_height="100dp"
android:background="@android:color/transparent" />
</LinearLayout>
EMail.java(项目类):
public class EMail {
public String title;
public boolean read;
public EMail(String title, boolean read) {
this.title = title;
this.read = read;
}
}
活动 onCreate 方法:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ListView lv = (ListView)findViewById(R.id.listView1);
ListAdapter emails = new ListAdapter(this, 0, new ArrayList<EMail>());
emails.add(new EMail("Email 1", false));
emails.add(new EMail("Email 2", false));
emails.add(new EMail("Email 3", true));
emails.add(new EMail("Email 4", true));
emails.add(new EMail("Email 5", false));
emails.add(new EMail("Email 6", false));
emails.add(new EMail("Email 7", true));
emails.add(new EMail("Email 8", false));
emails.add(new EMail("Email 9", false));
emails.add(new EMail("Email 10", true));
emails.add(new EMail("Email 11", false));
emails.add(new EMail("Email 12", false));
emails.add(new EMail("Email 13", false));
emails.add(new EMail("Email 14", true));
emails.add(new EMail("Email 15", false));
emails.add(new EMail("Email 16", false));
emails.add(new EMail("Email 17", false));
emails.add(new EMail("Email 18", false));
emails.add(new EMail("Email 19", false));
emails.add(new EMail("Email 20", false));
lv.setAdapter(emails);
}
适配器:
import java.util.ArrayList;
import java.util.Timer;
import java.util.TimerTask;
import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;
public class ListAdapter extends ArrayAdapter<EMail> {
private Timer timer = new Timer();
private int identifier = 0;
public ListAdapter(Context context, int textViewResourceId,
ArrayList<EMail> items) {
super(context, textViewResourceId, items);
}
@Override
public View getView(final int position, View v, ViewGroup parent) {
if (v == null) {
LayoutInflater vi = (LayoutInflater)getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(R.layout.email, null);
}
final int id = identifier++;
v.setTag(id);
TextView titleTextView = (TextView)v.findViewById(R.id.title);
final LinearLayout back = (LinearLayout)v.findViewById(R.id.background);
final EMail e = getItem(position);
titleTextView.setText(e.title);
if (e.read)
back.setBackgroundColor(getContext().getResources().getColor(R.color.gray));
else
{
back.setBackgroundColor(getContext().getResources().getColor(R.color.white));
final View view = v;
TimerTask task = new TimerTask() {
public void run() {
if (view.getTag().toString().equals(id+""))
{
e.read = true;
((Activity) getContext()).runOnUiThread(new Runnable() {
public void run() {
back.setBackgroundColor(getContext().getResources().getColor(R.color.gray));
}
});
}
}
};
timer.schedule(task, 3000);
}
return v;
}
}
You can set OnScrollListener to your ListView and check when user scrolls to your new items.