2013-05-19 147 views
0

工作,我必須在我的列表視圖頂部的按鈕。在我從列表中選擇一個項目之前,這些按鈕不起作用。據一些其他職位,我應該在以下行添加到我的XML文件:機器人:可調焦=「假」。但是,添加該行後沒有發生任何更改。按鈕不能與列表視圖

這裏是我的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

Button是您的onListItemClick所以內實例化他們直到你點擊一個列表項後纔會是clickcable。你可以將它們移動到onCreate()但隨後你將需要inflate另一個View在那裏還,使用setContentView()

此外,如由buptcoder評論說,我沒有看到附着在ButtonOnClickListener。我仍然沒有看到它是可點擊的,直到將它從現在的聽衆中移出後,仍然需要將OnClickListener連接到Button

+0

我有onClickListener代碼,但顯然沒有在我的文章中包括它。 (我的錯誤)的按鈕做移動出來的onListItemClick,進入我的onCreate的活動後具有焦點。謝謝你的幫助。 – sowens

+0

不客氣。很高興能幫到您 – codeMagic

1

這些按鈕不適用於任何事物,而不僅僅是列表顯示。

要獲得的點擊次數(我假設這是你想要的)工作,有你的活動實施View.OnClickListener並實現在onclick方法你的點擊處理邏輯(這裏更詳細https://developer.android.com/guide/topics/ui/ui-events.html),並在OnCreate,做這個:

Button b = (Button) findViewById(R.id.concert); 
b.setOnClickListener(this); 
+0

感謝您的幫助。我將onClick的代碼移至最初調用的活動。我有2項活動,所以我必須確定正確的一項。現在按鈕有焦點和onClick作品。 – sowens