【问题标题】:List View not scrolling in fragment列表视图不在片段中滚动
【发布时间】:2015-10-28 08:55:20
【问题描述】:

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

代码如下:

主要活动

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);
}

}

和弦片段

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 中滚动列表视图?

【问题讨论】:

    标签: listview android-fragments


    【解决方案1】:

    效果很好,如果你修改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>
    

    【讨论】:

    • 如果您已经解释了 OP 代码的问题以及您所做的更改,那将会很有帮助。现在,读者必须向上和向下、向上和向下查看以尝试了解差异是什么,并尝试猜测这些更改是否与相似但不相同的问题相关。
    猜你喜欢
    • 1970-01-01
    • 2011-10-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-20
    • 1970-01-01
    • 1970-01-01
    • 2019-03-23
    相关资源
    最近更新 更多