我的listview顶部有按钮。在从列表中选择项目之前,按钮不起作用。根据其他一些帖子,我应该在我的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();
}
}
答案 0 :(得分:1)
这些按钮不适用于任何内容,而不仅仅是ListView。
要获得点击(我假设这是您想要的),请让您的Activity实现View.OnClickListener并在onClick方法中实现您的点击处理逻辑(更多详细信息请https://developer.android.com/guide/topics/ui/ui-events.html),并在onCreate,执行此操作:
Button b = (Button) findViewById(R.id.concert);
b.setOnClickListener(this);
答案 1 :(得分:0)
您的Button
已在onListItemClick
内实例化,因此在您点击列表项之前它们不会是clickcable
。您可以将它们移至onCreate()
,但之后您还需要inflate
其他View
,使用setContentView()
此外,正如buptcoder的评论中所述,我看不到OnClickListener
附加了Button
。我仍然看不到它是可点击的,直到你将它移出听众现在,但OnClickListener
仍然需要附加到Button