gridview中的焦点问题

时间:2012-07-26 10:39:56

标签: android android-layout android-intent

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

我有以下要求。默认情况下,当我启动或恢复活动时,我需要默认选择一个imageview。我怎么能做到这一点?在 onResume()中放置 gridView.setSelection(1); 无效,即;启动或恢复时没有选择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;
}

}

2 个答案:

答案 0 :(得分:4)

你可以试试这个:

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

或者这也有帮助:

public void onResume(){

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

}

答案 1 :(得分: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