2015-10-28 105 views
1

我創建了抽屜。在選擇一個項目時,它將顯示一個帶有List視圖的片段。該列表視圖不滾動。列表視圖不滾動片段

下面是代碼:

主要活動

private NavigationDrawerFragment mNavigationDrawerFragment; 
private CharSequence mTitle; 
FrameLayout frameLayout; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    frameLayout = (FrameLayout)findViewById(R.id.container); 

    mNavigationDrawerFragment = (NavigationDrawerFragment) 
      getSupportFragmentManager().findFragmentById(R.id.navigation_drawer); 
    mTitle = getTitle(); 

    mNavigationDrawerFragment.setUp(
      R.id.navigation_drawer, 
      (DrawerLayout) findViewById(R.id.drawer_layout)); 
} 

@Override 
public void onNavigationDrawerItemSelected(int position) { 
    // update the main content by replacing fragments 
    FragmentManager fragmentManager = getSupportFragmentManager(); 
    FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction(); 
    switch (position) { 
     case 1: 
      break; 
     case 2: 

      Fragment f = new ChordFragment(); 

      fragmentTransaction.replace(R.id.container, f).commit(); 
      break; 
     case 3: 
      break; 
     case 4: 
      break; 
    } 

} 

public void onSectionAttached(int number) { 
    switch (number) { 
     case 1: 
      mTitle = getString(R.string.navi_home); 
      break; 
     case 2: 
      mTitle = getString(R.string.navi_favourite); 
      break; 
     case 3: 
      mTitle = getString(R.string.navi_new); 
      break; 
     case 4: 
      mTitle = getString(R.string.navi_rateus); 
      break; 
    } 
} 

public void restoreActionBar() { 
    ActionBar actionBar = getSupportActionBar(); 
    actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD); 
    actionBar.setDisplayShowTitleEnabled(true); 
    actionBar.setTitle(mTitle); 
} 


@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    if (!mNavigationDrawerFragment.isDrawerOpen()) { 
     // Only show items in the action bar relevant to this screen 
     // if the drawer is not showing. Otherwise, let the drawer 
     // decide what to show in the action bar. 
     getMenuInflater().inflate(R.menu.main, menu); 
     restoreActionBar(); 
     return true; 
    } 
    return super.onCreateOptionsMenu(menu); 
} 

@Override 
public boolean onOptionsItemSelected(MenuItem item) { 
    // Handle action bar item clicks here. The action bar will 
    // automatically handle clicks on the Home/Up button, so long 
    // as you specify a parent activity in AndroidManifest.xml. 
    int id = item.getItemId(); 

    //noinspection SimplifiableIfStatement 
    if (id == R.id.action_settings) { 
     return true; 
    } 

    return super.onOptionsItemSelected(item); 
} 

}

CHORDFRAGMENT

public class ChordFragment extends Fragment { 

ListView mChordList; 
ArrayList<TCDataModel> list = new ArrayList<TCDataModel>(); 

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
    /** Inflating the layout for this fragment **/ 
    View v = inflater.inflate(R.layout.fragment_chord, null); 

    mChordList = (ListView) v.findViewById(R.id.lv_chords); 
    for (int i = 0; i < 20; i++) { 
     TCDataModel mTCDataModel = new TCDataModel(); 
     mTCDataModel.setSongName("Song Name " + i); 
     mTCDataModel.setMuviName("Muvi Name " + i); 
     list.add(mTCDataModel); 
    } 
    mChordList.setAdapter(new ChordListAdapter(getContext(), list)); 
    return v; 
} 

}

ACTIVITY_MAIN

<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:id="@+id/drawer_layout" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
tools:context=".MainActivity"> 

<include 
    android:id="@+id/tool_bar" 
    layout="@layout/toolbar"> 

</include> 

<FrameLayout 
    android:id="@+id/container" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" /> 


<fragment 
    android:id="@+id/navigation_drawer" 
    android:layout_width="@dimen/navigation_drawer_width" 
    android:layout_height="match_parent" 
    android:layout_gravity="start" 
    tools:layout="@layout/fragment_navigation_drawer" /></android.support.v4.widget.DrawerLayout> 

fragment_chord

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 

<ListView 
    android:id="@+id/lv_chords"`enter code here` 
    android:layout_width="match_parent" 
    android:clickable="true" 
    android:layout_height="match_parent" />`</LinearLayout>` 

我覺得列表視圖中獲取抽屜從和絃片列表視圖中的觸摸事件。這就是爲什麼它不滾動。 有沒有辦法在Chord Fragment中滾動列表視圖?

回答

0

它運作良好,如果你修改main_activity.xml文件,如下所示:

<android.support.v4.widget.DrawerLayout  xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:id="@+id/drawer_layout" 
android:layout_width="match_parent" 
android:layout_height="match_parent" > 


<FrameLayout 
     android:id="@+id/container" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" /> 


<fragment 
     android:id="@+id/navigation_drawer" 
     android:layout_width="@dimen/navigation_drawer_width" 
     android:layout_height="match_parent" 
     android:layout_gravity="start" 
     tools:layout="@layout/fragment_navigation_drawer" /> 

</android.support.v4.widget.DrawerLayout> 
+2

如果你曾解釋什麼是錯與OP的代碼和它是什麼,你已經改變了這將是有益的。現在,讀者不得不查看上下和上下,以試圖查看差異是什麼,並嘗試猜測這些更改是否與類似但不完全相同的問題相關。 – RenniePet