安卓解决viewPager和scrollView和listView滑动冲突的问题

大家想想listView的实现方式 就是一个item一个item 添加到一个布局中,

那么LinearLayout可不可以像ListView 那样 往里面添加item  答案是可以的

我们先模拟listView 的LinearLayout类


public class LinearLayoutForListView extends LinearLayout {

    private ListAdapter adapter;
    private OnClickListener onClickListener = null;
    private OnTouchListener onTouchListener = null;


    /**
     * 绑定布局
     */
    public void bindLinearLayout() {
        int count = adapter.getCount();
        for (int i = 0; i < count; i++) {
            View v = adapter.getView(i, null, null);
            
            v.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
            v.setOnTouchListener(this.onTouchListener);
            v.setOnClickListener(this.onClickListener);
            v.setId(i);
            addView(v, i);
        }
        Log.v("countTAG", "" + count);
    }

    public LinearLayoutForListView(Context context) {
        super(context);
    }

    public LinearLayoutForListView(Context context, AttributeSet attrs) {
        super(context, attrs);
        // TODO Auto-generated constructor stub
    }

    /**
     * 获取Adapter
     *
     * @return adapter
     */
    public ListAdapter getAdpater() {
        return adapter;
    }

    /**
     * 设置数据
     *
     * @param adpater
     */
    public void setAdapter(ListAdapter adpater) {
        this.adapter = adpater;
        bindLinearLayout();
    }

    /**
     * 获取点击事件
     *
     * @return
     */
    public OnClickListener getOnclickListner() {
        return onClickListener;
    }

    /**
     * 设置点击事件
     *
     * @param onClickListener
     */
    public void setOnclickLinstener(OnClickListener onClickListener) {
        this.onClickListener = onClickListener;
    }
    
    public OnTouchListener getOnTouchListener() {
        return onTouchListener;
    }

    public void setOnTouchListener(OnTouchListener onTouchListener) {
        this.onTouchListener = onTouchListener;
    }

}



这就好比一个listView


使用的时候


 <com.groupbuy.view.LinearLayoutForListView  
            android:id="@+id/linlistview"  
            android:layout_width="fill_parent"  
            android:layout_height="wrap_content"  
            android:scrollbars="vertical"  
            android:orientation="vertical"  
            android:paddingLeft="5dp"  
            android:paddingRight="5dp"  
            android:paddingTop="3dp"  
            android:paddingBottom="30dp"  
            android:background="#ffFFFF">  
       
   </com.groupbuy.view.LinearLayoutForListView> 


adapter 和平常listView 一样 


设置adapter

    LinearLayoutForListView mSetupList = (LinearLayoutForListView) findViewById(R.id.linlistview);
      mSetupList.setOnclickLinstener(SetupListClickEvent);
      mSetupList.setOnTouchListener(SetupListTouchEvent);
      mSetupList.setAdapter(new MainListAdpter(this));


//触摸事件处理

View.OnTouchListener SetupListTouchEvent = new View.OnTouchListener() {
        
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            // TODO Auto-generated method stub            
            return false;
        }
    };
//点击事件处理
    View.OnClickListener SetupListClickEvent = new View.OnClickListener()
    {
        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub            
            int position = v.getId();
            Intent intent=new Intent(HomeActivity.this,ShopDetail.class);
            startActivity(intent);
        }
    };



ok  这样就可以避免冲突啦

<ScrollView  
     android:layout_width="fill_parent"  
     android:layout_height="wrap_content">  
    <LinearLayout
        android:layout_width="fill_parent"  
     android:layout_height="wrap_content"
     android:orientation="vertical"
        >
        <RelativeLayout  
        android:layout_width="fill_parent"  
        android:layout_height="200dp"
         >  
    
          <android.support.v4.view.ViewPager  
        android:id="@+id/viewPager"  
        android:layout_width="fill_parent"  
        android:layout_height="match_parent" />
    
 
        <LinearLayout  
            android:id="@+id/viewGroup"  
            android:layout_width="fill_parent"  
            android:layout_height="10dip"  
            android:layout_alignParentBottom="true"
            android:layout_marginBottom="5dip"  
            android:layout_marginRight="10dip"
            android:gravity="right"  
            android:orientation="horizontal" >  
        </LinearLayout>  
    </RelativeLayout>
    <LinearLayout
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
           android:layout_marginTop="1dp"
          android:background="@drawable/more_item_press"
        >
     <LinearLayout
                
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
               
                android:background="@drawable/account_tab_bg"
                android:orientation="horizontal" >

                <TextView
                    android:id="@+id/My_checkin"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:layout_margin="8dp"
                    android:layout_weight="1"
                     
                    android:gravity="center"
                    android:padding="10dp"
                    android:text="洗车"
                    android:textColor="#666666"
                    android:textSize="17sp" />

                <TextView
                    android:id="@+id/My_comment"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:layout_margin="8dp"
                    android:layout_weight="1"
              
                    android:gravity="center"
                    android:padding="10dp"
                    android:text="补胎"
                    android:textColor="#666666"
                    android:textSize="17sp" />

                <TextView
                    android:id="@+id/My_photo"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:layout_margin="8dp"
                    android:layout_weight="1"
                    
                    android:gravity="center"
                    android:padding="10dp"
                    android:text="流动补胎"
                    android:textColor="#666666"
                    android:textSize="17sp" />
            </LinearLayout>
            </LinearLayout>
    <com.groupbuy.view.LinearLayoutForListView  
            android:id="@+id/linlistview"  
            android:layout_width="fill_parent"  
            android:layout_height="wrap_content"  
            android:scrollbars="vertical"  
            android:orientation="vertical"  
            android:paddingLeft="5dp"  
            android:paddingRight="5dp"  
            android:paddingTop="3dp"  
            android:paddingBottom="30dp"  
            android:background="#ffFFFF">  
       
   </com.groupbuy.view.LinearLayoutForListView>  

   </LinearLayout>  
</ScrollView> 

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值