我想在每个GridView单元上显示相同的图像,但是每个之后都需要两到三秒钟。请尝试帮助我。提前致谢
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;
}
}
这是我的代码。我想要这样的输出。
This is the output which I am expecting
最佳答案
这是您要求的完整工作示例。
在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>
关于android - 每次两秒钟后一一显示GridView单元格,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/35865780/