2015-05-04 63 views
0

我把一個ViewPager放在DrawerLayout中。 現在,當我嘗試滾動ViewPager,菜單關閉DrawerLayout中的ViewPager

enter image description here

請告訴我如何讓菜單不會關閉,當我滾動viewPager

viewPager適配器:

public class MenuNewsAdapter extends PagerAdapter { 

ArrayList<News> news = new ArrayList<>(); 
Context context; 

public MenuNewsAdapter(Context context, ArrayList<News> news) { 
    this.news = news; 
    this.context = context; 
} 

@Override 
public int getCount() { 
    return news.size(); 
} 


@Override 
public Object instantiateItem(final ViewGroup container, final int position) { 
    LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
    final View rowView = inflater.inflate(R.layout.list_menu_news, container, false); 

    ... 

    rowView.setOnClickListener(new View.OnClickListener() { 
     public void onClick(View v) { 
      Intent intent = new Intent(container.getContext(), NewsViewActivity.class); 
      intent.putExtra("news_id", news.get(position).newsId); 
      container.getContext().startActivity(intent); 
     } 
    }); 

    container.addView(rowView); 
    return rowView; 
} 

@Override 
public boolean isViewFromObject(View view, Object object) { 
    return view == ((LinearLayout) object); 
} 
@Override 
public void destroyItem(ViewGroup container, int position, Object object) { 
    container.removeView((LinearLayout) object); 
} 
} 

回答

1

只需添加一個onTouchListener。當您觸摸ViewPager(action_down)上的屏幕時,不允許父母攔截該事件。

viewPager.setOnTouchListener(new View.OnTouchListener() { 
    @Override 
    public boolean onTouch(View v, MotionEvent event) { 
     int action = event.getAction(); 
     switch (action) { 
     case MotionEvent.ACTION_DOWN: 
      // Disallow Drawer to intercept touch events. 
      v.getParent().requestDisallowInterceptTouchEvent(true); 
      break; 

     case MotionEvent.ACTION_UP: 
      // Allow Drawer to intercept touch events. 
      v.getParent().requestDisallowInterceptTouchEvent(false); 
      break; 
     } 

     // Handle viewPager touch events. 
     v.onTouchEvent(event); 
     return true; 
    } 
}); 
+0

謝謝。但當我添加點擊監聽器 「rowView.setOnClickListener(new View.OnClickListener(){ public void onClick(View v){」 ,我再次得到問題 –

+0

什麼是rowView? – dumazy

+0

我在第一條消息中添加了代碼 –