列表视图不在片段中滚动

时间:2015-10-28 08:55:21

标签: listview android-fragments

我创造了抽屉。在选择一个项目时,它将显示带有列表视图的片段。该列表视图不滚动。

以下是代码:

主要活动

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中的列表视图中获取触摸事件。这就是为什么它不滚动。 有没有办法在Chord Fragment中滚动列表视图?

1 个答案:

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