2015-06-18 46 views
1

請幫助我以下情況。我們怎樣才能滾動父母的佈局,同時滾動recyclerview android

我正在開發一個應用程序,其中在一個頁面中我有一個Linearlayout與背景圖像和RecyclerView與名稱列表。我需要的是當我向上滾動RecyclerView時我需要上面的LinearLayout也向上移動,以便recyclerView中的列表並不在上面的LinearLayout下,當我向下滾動RecyclerView時,我需要上面的LinearLayout向下滾動,以便我們可以完全看到圖像。

我已經做過的事情是我使用了recyclerview的setOnScrollListener,並且在onScrolled()函數中,我正在向下滾動並向上滾動事件。但現在我被卡住瞭如何繼續下去。

下面是佈局:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:card_view="http://schemas.android.com/apk/res-auto" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:fitsSystemWindows="true"> 

    <include layout="@layout/toolbar" 
     android:id="@+id/toolbar" /> 

    <android.support.v7.widget.CardView 
     android:id="@+id/cardview" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_below="@+id/toolbar" 
     android:layout_margin="8dp" 
     card_view:cardBackgroundColor="@android:color/white" 
     card_view:cardCornerRadius="8dp"> 

      <ScrollView 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" 
       android:id="@+id/scrollview" 
       android:fillViewport="true"> 

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

         <LinearLayout 
          android:id="@+id/map" 
          android:layout_width="match_parent" 
          android:layout_height="0dp" 
          android:orientation="vertical" 
          android:background="@drawable/hydeparkmap" 
          android:layout_weight="3" ></LinearLayout> 


         <android.support.v7.widget.RecyclerView 
          android:id="@+id/recycler_view" 
          android:layout_width="match_parent" 
          android:layout_height="0dp" 
          android:layout_weight="2" 
          android:scrollbars="vertical" /> 

        </LinearLayout> 

      </ScrollView> 

    </android.support.v7.widget.CardView> 

</RelativeLayout> 

下面是代碼,我在相應的Java類中使用:

@InjectView(R.id.scrollview) 
    ScrollView scrollview; 

    protected void onCreate(Bundle savedInstanceState) { 


     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_shoplist); 
     ButterKnife.inject(this); 

     setSupportActionBar(toolbar); 
     getSupportActionBar().setDisplayHomeAsUpEnabled(true); 
     getSupportActionBar().setHomeButtonEnabled(true); 
     getSupportActionBar().setTitle("ShopList"); 

     scrollview.setVerticalScrollBarEnabled(true); 
     lm=new LinearLayoutManager(ShopListActivity.this); 
     recyclerView.setLayoutManager(lm); 
     firstVisibleInListview = lm.findFirstVisibleItemPosition(); 
     adapter = new RecyclerViewAdapter(ShopListActivity.this, getData()); 
     recyclerView.setAdapter(adapter); 

     recyclerView.addOnItemTouchListener(
       new RecyclerItemClickListener(ShopListActivity.this, new RecyclerItemClickListener.OnItemClickListener() { 
        @Override 
        public void onItemClick(View view, int position) { 

         Intent intent1 = new Intent(ShopListActivity.this, ShopProductListActivity.class); 
         intent1.putExtra("position", String.valueOf(position)); 
         intent1.putExtra("shopname", it.get(position).getTitle()); 
         intent1.putExtra("shopimage", String.valueOf(it.get(position).getImgIcon())); 
         intent1.putExtra("subcategory", subcategory); 
         startActivity(intent1); 

        } 
       }) 
     ); 
     recyclerView.setOnScrollListener(new RecyclerView.OnScrollListener() { 
      @Override 
      public void onScrolled(RecyclerView recyclerView, int dx, int dy) { 
       super.onScrolled(recyclerView, dx, dy); 
       int currentFirstVisible = lm.findFirstVisibleItemPosition(); 

       if(currentFirstVisible > firstVisibleInListview){ 
        Toast.makeText(getBaseContext(),"Scrolled up ",Toast.LENGTH_SHORT).show(); 
        scrollview.fullScroll(ScrollView.FOCUS_UP); 
       } 
       else 
        Toast.makeText(getBaseContext(),"Scrolled down ",Toast.LENGTH_SHORT).show(); 

       firstVisibleInListview = currentFirstVisible; 
      } 
     }); 

    } 
+0

使用此類型的視圖。作出兩個看法即。一個帶有你希望顯示的標題。其他的東西將以另一種觀點出現。現在我正在檢查其他的東西,所以不寫代碼。但給鏈接。結帳[鏈接](http://www.android4devs.com/2015/01/recycler-view-handling-onitemtouch-for.html) –

回答

2

,所以如果我完全理解你的問題,你一定查看應該滾動 RcyclerView。 如果是的話,在這種情況下你應該刪除你的ScrollView,因爲RecyclerView本身就有這個功能。我在這裏寫的是一步一步的指導,把標題(不粘)放在你的RecyclerView頂部,這樣他們將滾動到一起:

1-首先你需要創建一個包含你的標題的佈局,在我的情況下,我有一個對於頭部圖像,它看起來是這樣的:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:orientation="vertical" android:layout_width="match_parent" 
android:layout_height="match_parent"> 

<ImageView 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:src="@mipmap/winter"/> 

</LinearLayout> 

我們稱之爲header_layout

2 - 你需要實現RecyclerAdapter的另一種方法叫做getItemViewType它的輸出會作爲第二個參數onCreateViewHolder(ViewGroup中的父母,INT viewType)在這裏你膨脹你需要的每種視圖的佈局(對於我來說,這兩個看起來是李這個):

@Override 
public int getItemViewType(int position){ 
    if(position == 0){ 
     return 0; 
    } else { 
     return 1; 
    } 
} 

@Override 
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { 
    View itemView; 
    if(viewType==1){ 
     itemView = LayoutInflater.from(parent.getContext()) 
       .inflate(R.layout.report_row_layout, parent, false); 
    } else { 
     itemView = LayoutInflater.from(parent.getContext()) 
       .inflate(R.layout.report_header_layout, parent, false); 
    } 

    return new MyViewHolder(itemView); 
} 

請注意,我的第一個職位(標題)不同,其他是相同的,你可以有多個職位的多個意見。

3-如果需要的話在我的情況下,你應該改變你onBindViewHolder我需要讓我的第一個位置

4-刪除您滾動型什麼也不做,實現位置佈局和主佈局應該是這樣的:

<RelativeLayout 
xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:card_view="http://schemas.android.com/apk/res-auto" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:fitsSystemWindows="true"> 

<include layout="@layout/toolbar" 
    android:id="@+id/toolbar" /> 

<android.support.v7.widget.CardView 
    android:id="@+id/cardview" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:layout_below="@+id/toolbar" 
    android:layout_margin="8dp" 
    card_view:cardBackgroundColor="@android:color/white" 
    card_view:cardCornerRadius="8dp"> 

    <android.support.v7.widget.RecyclerView 
     android:id="@+id/recycler_view" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" /> 

</android.support.v7.widget.CardView> 

</RelativeLayout> 

我不知道你的CardView在做什麼,但是如果你認爲它不需要的話就刪除它。如果你需要的話,你可以使你的標題爲LinearLayout或其他任何東西。

希望這有助於。

相關問題