簡體   English   中英

如何在兩個列表片段之間添加一個?

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

我想添加兩個listfragments(listfragment1和listfragment2),它們的列表是通過webservice調用動態填充的。 假設listfragment2恰好位於listfragment1的下方,因此,如果在listfragment1中獲取了更多內容,則listfragment2應該向下移動/滑動。

為什么不在線性布局中包裝兩個列表片段。 您應該為每個列表片段使用兩個不同的適配器。 這更像是在一個活動中支持兩個片段以獲得更大的屏幕。 一個帶有頁面標題,另一個帶有頁面細節。

檢查此示例以開始。Link- 支持平板電腦。

主要布局:

<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>

在活動中添加片段:

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

進行自定義listview並阻止垂直滾動。

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

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM