我希望在每个GridView Cell上显示相同的图像,但每次在两到三秒后显示。请尽力帮助我。提前致谢
PlayActivity.java
public class PlayActivity extends AppCompatActivity
{
private Intent intent;
private GridView gridView;
private int rangeFrom, rangeTo, counter, imageId=R.drawable.horse;
private String order;
private String[] list;
private Handler handler;
private int i;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_play);
gridView = (GridView) findViewById(R.id.gridView);
intent=getIntent();
if(intent.getStringExtra("rangeFrom")!=null && intent.getStringExtra("rangeTo")!=null)
{
rangeFrom=Integer.parseInt(intent.getStringExtra("rangeFrom"));
rangeTo=Integer.parseInt(intent.getStringExtra("rangeTo"));
}
counter=Integer.parseInt(intent.getStringExtra("counter"));
order=intent.getStringExtra("order");
Thread t1=new Thread(){
public void run() {
try {
for(i= 1; i<=5; i++) {
gridView.deferNotifyDataSetChanged();
gridView.setAdapter(new PlayCustomAdapter(PlayActivity.this, i, imageId));
wait(2000);
gridView=null;
gridView=(GridView) findViewById(R.id.gridView);
}
}
catch (Exception e) {
e.printStackTrace();
}
}
};
t1.start();
}
}
PlayCustomAdapter.java
public class PlayCustomAdapter extends BaseAdapter
{
private Context context;
private int imageId, rangeTo;
private static LayoutInflater inflater=null;
public PlayCustomAdapter(PlayActivity playActivity, int rangeTo, int imageId) {
context=playActivity;
this.rangeTo=rangeTo;
this.imageId=imageId;
inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public int getCount() {
return rangeTo;
}
@Override
public Object getItem(int position) {
return position;
}
@Override
public long getItemId(int position) {
return position;
}
public class Holder
{
ImageView iv;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
Holder holder=new Holder();
View rowview;
rowview=inflater.inflate(R.layout.play_gridview_list, null);
holder.iv=(ImageView) rowview.findViewById(R.id.imageView1);
holder.iv.setImageResource(imageId);
return rowview;
}
}
这是我的代码。我想要这样的输出。
答案 0 :(得分:1)
使用post delay handler
public View getView(int position, View convertView, ViewGroup parent) {
Holder holder=new Holder();
View rowview;
rowview=inflater.inflate(R.layout.play_gridview_list, null);
holder.iv=(ImageView) rowview.findViewById(R.id.imageView1);
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
holder.iv.setImageResource(imageId);
}
},2000);
return rowview;
}
答案 1 :(得分:0)
以下是您的要求的完整工作示例。
在 MainActivity.java 中,
public class MainActivity extends AppCompatActivity
{
private GridView gridView;
private ArrayList<Integer> imagesList;
private PlayCustomAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
gridView = (GridView) findViewById(R.id.gridView);
gridView.deferNotifyDataSetChanged();
imagesList = new ArrayList<Integer>();
adapter = new PlayCustomAdapter(imagesList);
gridView.setAdapter(adapter);
// By using AsyncTask
new LoadImagesTask().execute();
}
private class PlayCustomAdapter extends BaseAdapter
{
private ArrayList<Integer> imagesList;
private LayoutInflater inflater = null;
public PlayCustomAdapter(ArrayList<Integer> imagesList)
{
this.imagesList = imagesList;
inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public int getCount()
{
return imagesList.size();
}
@Override
public Object getItem(int position)
{
return imagesList.get(position);
}
@Override
public long getItemId(int position)
{
return position;
}
public class Holder
{
ImageView iv;
}
@Override
public View getView(int position, View convertView, ViewGroup parent)
{
Holder holder = new Holder();
View rowview;
rowview = inflater.inflate(R.layout.play_gridview_list, null);
holder.iv = (ImageView) rowview.findViewById(R.id.imageView1);
holder.iv.setImageResource((int) getItem(position));
return rowview;
}
}
private class LoadImagesTask extends AsyncTask<Void, Integer, Void>
{
@Override
protected Void doInBackground(Void... voids)
{
try
{
for (int i = 1; i <= 5; i++)
{
// Here, I am getting launcher icon from resources.
publishProgress(R.mipmap.ic_launcher);
Thread.sleep(2000);
}
}
catch (Exception ex)
{
ex.printStackTrace();
}
return null;
}
@Override
protected void onProgressUpdate(Integer... values)
{
super.onProgressUpdate(values);
imagesList.add(values[0]);
adapter.notifyDataSetChanged();
}
}
}
在 activity_main.xml 中,
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="esealed.com.testing123.MainActivity">
<GridView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/gridView"/>
</RelativeLayout>
在 play_gridview_list.xml ,
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/imageView1"/>
</LinearLayout>