0

我的布局中有一个gridview。我在这个 gridview 中添加了三个图像视图。

我有以下要求。默认情况下,当我启动或恢复活动时,我需要默认选择一个图像视图。我怎样才能做到这一点?放置gridView.setSelection(1); onResume()中没有效果,即;imageview 在启动或恢复时没有被选中,我想知道为什么!有人可以告诉我为什么没有突出显示吗?

以下是我的主要活动和适配器类。

public class MyActivity extends Activity
{
GridView gridView;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    gridView = (GridView) findViewById(R.id.grid_view);
    // Instance of ImageAdapter Class
    gridView.setAdapter(new ImageAdapter(this));
    // set up the onClick listener
    gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View v,
                                int position, long id) {
            Log.d("Pos", "setOnItemClickListener() - pos:" + position);
            handleItemClick(position);
        }
    });

}
public void onResume() {
    super.onResume();
    gridView.setSelection(1);
}
private void handleItemClick(int position) {

        Intent intent = new Intent();
        intent.setAction(Intent.ACTION_VIEW);

        switch(position) {
            case 0:  // Phone
                intent.setClassName("com.android.contacts",
                        "com.android.contacts.DialtactsActivity");
                break;
            case 1:  // People
                intent.setClassName("com.android.contacts",
                        "com.android.contacts.activities.PeopleActivity");
                intent.setAction("com.android.contacts.action.LIST_CONTACTS");
                break;
            case 2:  // Places
                intent.setClassName("com.android.contacts",
                        "com.android.contacts.DialtactsActivity");
                break;
            default:
                break;
        }
        startActivity(intent);
}
}

适配器类

public class ImageAdapter extends BaseAdapter {
private Context mContext;

// Keep all Images in an array   
public Integer[] mThumbIds = {
    R.drawable.call, 
    R.drawable.contacts,
    R.drawable.music,
};

public ImageAdapter(Context c){
    mContext = c;
}

@Override
public int getCount() {
    return mThumbIds.length;
}

@Override
public Object getItem(int position) {
    return mThumbIds[position];
}

@Override
public long getItemId(int position) {
    return position;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    ImageView imageView; 

    if (convertView == null) {           
        imageView = new ImageView(mContext);  
        imageView.setLayoutParams(new GridView.LayoutParams(350, 350));  
    } else {  
        imageView = (ImageView) convertView;   
    }

    imageView.setImageResource(mThumbIds[position]);
    imageView.setId(position);

    return imageView;
}

}
4

2 回答 2

4

你可以试试这个:

public void onResume() {
    super.onResume();
    Thread.sleep(1);
    gridview.requestFocusFromTouch();
    gridview.setSelection(1); 
}

或者这也可以帮助:

公共无效 onResume() {

super.onResume();
mGridView.setSelection(1);
mGridView.requestFocusFromTouch();
mGridView.setSelection(1);

}

于 2012-07-26T11:02:54.140 回答
1

只是猜测可能有效

    setContentView(R.layout.main);
    gridView = (GridView) findViewById(R.id.grid_view);
    // Instance of ImageAdapter Class
    gridView.setAdapter(new ImageAdapter(this));
    gridView.setSelection(1); <--- try to add this here
于 2012-07-26T10:43:22.447 回答