简体   繁体   中英

How can I add two list fragments one below the other?

I want to add two listfragments (listfragment1 and listfragment2) whose list are populated dynamically from webservice call. The listfragment2 is suppose to be just below the listfragment1 such that if more content is fetched in listfragment1 then listfragment2 should move/slide down.

Why not wrap two listfragments inside a linearlayout. you should use two different adapters for each listfragment. This is more like supporting two fragments in one activity for larger screen. One with the page titles and the other with page details.

Check this sample to start with.Link - supporting tablets.

Main Layout:

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical" >

    <LinearLayout
        android:id="@+id/lstFragment1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >
    </LinearLayout>


    <LinearLayout
        android:id="@+id/lstFragment2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >
    </LinearLayout>
</LinearLayout>

add fragments in activity:

         addFragment(R.id.lstFragment1, new lstFragment1());
         addFragment(R.id.lstFragment2, new lstFragment2());

         public void addFragment(int layoutId, Fragment fragment) {
           FragmentManager fm = getSupportFragmentManager();
           FragmentTransaction ft = fm.beginTransaction();
           ft.replace(layoutId, fragment);
           ft.commit();}

make custom listview with blocking verticle scrolling..

    public class IjoomerListView extends ListView {

@SuppressWarnings("unused")
private static final int SWIPE_MIN_DISTANCE = 50;
@SuppressWarnings("unused")
private static final int SWIPE_THRESHOLD_VELOCITY = 100;
@SuppressWarnings("unused")
private GestureDetector gDetector;
@SuppressWarnings("unused")
private boolean isFling;

private float mDiffX;
private float mDiffY;
private float mLastX;
private float mLastY;

/**
 * Overrides method
 */
@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
    switch (ev.getAction()) {
    case MotionEvent.ACTION_DOWN:
        // reset difference values
        mDiffX = 0;
        mDiffY = 0;

        mLastX = ev.getX();
        mLastY = ev.getY();
        break;

    case MotionEvent.ACTION_MOVE:
        final float curX = ev.getX();
        final float curY = ev.getY();
        mDiffX += Math.abs(curX - mLastX);
        mDiffY += Math.abs(curY - mLastY);
        mLastX = curX;
        mLastY = curY;

        // don't intercept event, when user tries to scroll vertically
        if (mDiffX < mDiffY) {
            return false; // do not react to horizontal touch events, these
                            // events will be passed to your list item view
        }
    }

    return super.onInterceptTouchEvent(ev);
}

public IjoomerListView(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
    init(context);
}

public IjoomerListView(Context context, AttributeSet attrs) {
    super(context, attrs);
    init(context);
}

public IjoomerListView(Context context) {
    super(context);
    init(context);
}

private void init(Context mContext) {
}

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM