我的列表视图顶部有按钮。在我从列表中选择一个项目之前,这些按钮不起作用。根据其他一些帖子,我应该将以下行添加到我的 XML 文件中:android:focusable="false"。但是,添加该行后没有发生任何变化。
这是我的 XML 文件:
<LinearLayout
android:layout_weight="2"
android:layout_height="fill_parent"
android:layout_width="match_parent"
>
<VideoView
android:id="@+id/video_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
/>
</LinearLayout>
<LinearLayout
android:layout_weight="1"
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:orientation="horizontal" >
<Button
android:id="@+id/chapters"
android:layout_width="wrap_content"
android:layout_height="@dimen/btn_layout_height"
android:textSize="@dimen/text_size"
android:background="@drawable/button_custom"
android:focusable="false"
android:text="@string/chapters" />
<Button
android:id="@+id/scales"
android:layout_width="100dp"
android:layout_height="@dimen/btn_layout_height"
android:textSize="@dimen/text_size"
android:background="@drawable/button_custom"
android:focusable="false"
android:text="@string/scales" />
<Button
android:id="@+id/b_flat"
android:layout_width="wrap_content"
android:layout_height="@dimen/btn_layout_height"
android:textSize="@dimen/text_size"
android:background="@drawable/button_custom"
android:text="@string/b_flat" />
<Button
android:id="@+id/e_flat"
android:layout_width="75dp"
android:layout_height="@dimen/btn_layout_height"
android:textSize="@dimen/text_size"
android:background="@drawable/button_custom"
android:text="@string/e_flat" />
<Button
android:id="@+id/concert"
android:layout_width="wrap_content"
android:layout_height="@dimen/btn_layout_height"
android:textSize="@dimen/text_size"
android:background="@drawable/button_custom"
android:text="@string/concert" />
<Button
android:id="@+id/bass"
android:layout_width="75dp"
android:layout_height="@dimen/btn_layout_height"
android:textSize="@dimen/text_size"
android:background="@drawable/button_custom"
android:text="@string/bass" />
<Button
android:id="@+id/video"
android:layout_width="wrap_content"
android:layout_height="@dimen/btn_layout_height"
android:textSize="@dimen/text_size"
android:background="@drawable/button_custom"
android:text="@string/video" />
<Button
android:id="@+id/practice"
android:layout_width="wrap_content"
android:layout_height="@dimen/btn_layout_height"
android:textSize="@dimen/text_size"
android:background="@drawable/button_custom"
android:text="@string/practice" />
</LinearLayout>
<FrameLayout
android:id="@+id/fragmentContainer"
android:layout_height="match_parent"
android:layout_width="match_parent"
/>
</LinearLayout>
这是我用来显示列表的代码:
public class ChapterListFragment extends ListFragment {
private static final boolean VERBOSE = true;
private ArrayList<Chapters> mChapters;
private static final String TAG = "ChapterListFragment";
private Button mChaptersButton;
@Override
public void onCreate(Bundle savedInstanceState) {
if (VERBOSE) Log.v(TAG, "+++ onCreate +++");
super.onCreate(savedInstanceState);
getActivity().setTitle(R.string.chapters_title);
mChapters = ChapterList.get(getActivity()).getChapters();
ChapterAdapter adapter = new ChapterAdapter(mChapters);
setListAdapter(adapter);
}
我将“onListItemClick”更新为以下内容:
@Override
public void onListItemClick(ListView l, View v, int position, long id) {
//if (VERBOSE) Log.v(TAG, "+++ onListItemClick +++");
// Get the chapter from the adapter
Chapters c = ((ChapterAdapter)getListAdapter()).getItem(position);
//Start ImprovisationActivity
Intent i = new Intent(getActivity(), ImprovisationActivity.class);
i.putExtra(ChapterFragment.EXTRA_CHAPTER_ID, c.getId());
startActivity(i);
}
private class ChapterAdapter extends ArrayAdapter<Chapters> {
public ChapterAdapter(ArrayList<Chapters> chapters) {
super(getActivity(), 0, chapters);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// If we weren't given a view, inflate one
if (convertView == null) {
convertView = getActivity().getLayoutInflater()
.inflate(R.layout.list_item_chapter, null);
}
// Configure the view for this Chapter
Chapters c = getItem(position);
TextView titleTextView =
(TextView) convertView.findViewById(R.id.chapter_list_item_titleTextView);
titleTextView.setText(c.getChapter());
return convertView;
}
}
@Override
public void onResume() {
super.onResume(); {
((ChapterAdapter)getListAdapter()).notifyDataSetChanged();
}
}
}
对于任何有同样问题的人,我在我的活动中添加了以下代码,现在我的按钮有了焦点:
public class ChapterListActivity extends SingleFragmentActivity {
private static final boolean VERBOSE = true;
private static final String TAG = "ChapterListActivity";
private Button mChaptersButton;
@Override
public void onCreate(Bundle savedInstanceState) {
if (VERBOSE) Log.v(TAG, "+++ onCreate +++");
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_improvisation);
mChaptersButton = (Button)findViewById(R.id.chapters);
// Need to default the button to pressed
mChaptersButton.setSelected(true);
mChaptersButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if (mChaptersButton.isSelected()){
if (VERBOSE) Log.v(TAG, "+++ onClick for mChaptersButton +++");
mChaptersButton.setSelected(false);
Toast.makeText(getApplicationContext(), "Chapters Button Not Selected",
Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(getApplicationContext(), "Chapters Button Selected",
Toast.LENGTH_SHORT).show();
mChaptersButton.setSelected(true);
}
}
});
}
@Override
protected Fragment createFragment() {
if (VERBOSE) Log.v(TAG, "+++ createFragment +++");
return new ChapterListFragment();
}
}