2016-03-21 4 views
-1

Я просто хочу отобразить список, используя вид перекоса в представлении пейджера. (например, как значки домашней страницы Android) Но здесь список не отображается, и без представления пейджер он работает.Android View Pager и RecycleView

Пожалуйста, может кто-нибудь сказать мне, где проблема ... Спасибо ..

activity.xml

<android.support.v4.view.ViewPager 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/pager" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" /> 

obj_tile.xml

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:orientation="vertical" 
    android:padding="5dp"> 

    <ImageView 
     android:id="@+id/iv_icon" 
     android:layout_width="50dp" 
     android:layout_height="50dp" 
     android:src="@android:drawable/alert_light_frame" /> 

    <TextView 
     android:id="@+id/tv_title" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:maxLines="2" 
     android:text="Group Title" /> 

</LinearLayout> 

-fragment_screen_slide_page.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/content" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

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

    <TextView 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:text="PageViewer Title" /> 

</RelativeLayout> 

MainActivity.java

public class MainActivity extends FragmentActivity { 
    private static final int NUM_PAGES = 5; 
    private ViewPager mPager; 
    private PagerAdapter mPagerAdapter; 
    List<TilePojo> tilePojoList=new ArrayList<>(); 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 



     // Instantiate a ViewPager and a PagerAdapter. 
     mPager = (ViewPager) findViewById(R.id.pager); 
     mPagerAdapter = new ScreenSlidePagerAdapter(getSupportFragmentManager()); 
     mPager.setAdapter(mPagerAdapter); 


     /*Context context=MainActivity.this; 
     LayoutInflater inflater = (LayoutInflater) context 
       .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     View v = inflater.inflate(R.layout.fragment_screen_slide_page, null); 
     RecyclerView rvTiles = (RecyclerView) findViewById(R.id.my_recycler_view); 

     for (int i = 0; i < 10; i++) { 
      TilePojo tilePojo = new TilePojo(R.drawable.apple, "Button Title " + i); 
      tilePojoList.add(tilePojo); 
     } 

     TileAdapter adapter = new TileAdapter(tilePojoList); 
     if(adapter!=null) { 
      rvTiles.setAdapter(adapter); 
      rvTiles.setLayoutManager(new GridLayoutManager(this, 2)); 
     } 
*/ 



    } 

    @Override 
    public void onBackPressed() { 
     if (mPager.getCurrentItem() == 0) { 
      // If the user is currently looking at the first step, allow the system to handle the 
      // Back button. This calls finish() on this activity and pops the back stack. 
      super.onBackPressed(); 
     } else { 
      // Otherwise, select the previous step. 
      mPager.setCurrentItem(mPager.getCurrentItem() - 1); 
     } 
    } 


    /** 
    * A simple pager adapter that represents 5 ScreenSlidePageFragment objects, in 
    * sequence. 
    */ 
    private class ScreenSlidePagerAdapter extends FragmentStatePagerAdapter { 
     public ScreenSlidePagerAdapter(FragmentManager fm) { 
      super(fm); 
     } 

     @Override 
     public Fragment getItem(int position) { 
      return new ScreenSlidePageFragment(); 
     } 

     @Override 
     public int getCount() { 
      return NUM_PAGES; 
     } 
    } 

} 

-ScreenSlidePageFragment.java

public class ScreenSlidePageFragment extends Fragment { 

    List<TilePojo> tilePojoList=new ArrayList<>(); 

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
          Bundle savedInstanceState) { 
     ViewGroup rootView = (ViewGroup) inflater.inflate(
       R.layout.fragment_screen_slide_page, container, false); 

     View v = inflater.inflate(R.layout.fragment_screen_slide_page, null); 
     RecyclerView rvTiles = (RecyclerView) v.findViewById(R.id.my_recycler_view); 

     for (int i = 0; i < 10; i++) { 
      TilePojo tilePojo = new TilePojo(R.drawable.apple, "Button Title " + i); 
      tilePojoList.add(tilePojo); 
     } 

     TileAdapter adapter = new TileAdapter(tilePojoList); 
     if(adapter!=null) { 
      rvTiles.setAdapter(adapter); 
      rvTiles.setLayoutManager(new GridLayoutManager(this.getActivity(), 2)); 
     } 


     return rootView; 
    } 
} 

TileAdapter.java

public class TileAdapter extends RecyclerView.Adapter<TileAdapter.ViewHolder> { 

    private List<TilePojo> list; 

    public TileAdapter(List<TilePojo> list) { 
     this.list = list; 
    } 

    @Override 
    public TileAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { 
     Context context = parent.getContext(); 
     LayoutInflater inflater = LayoutInflater.from(context); 

     // Inflate the custom layout 
     View tileView = inflater.inflate(R.layout.obj_tile, parent, false); 

     // Return a new holder instance 
     ViewHolder viewHolder = new ViewHolder(tileView); 
     return viewHolder; 
    } 

    @Override 
    public void onBindViewHolder(TileAdapter.ViewHolder holder, int position) { 
     TilePojo tilePojo=list.get(position); 

     holder.imageView.setImageResource(tilePojo.getSrc()); 
     holder.nameTextView.setText(tilePojo.getTitle()); 
    } 

    @Override 
    public int getItemCount() { 
     return list.size(); 
    } 

    public static class ViewHolder extends RecyclerView.ViewHolder { 
     // Your holder should contain a member variable 
     // for any view that will be set as you render a row 
     public TextView nameTextView; 
     public ImageView imageView; 

     // We also create a constructor that accepts the entire item row 
     // and does the view lookups to find each subview 
     public ViewHolder(View itemView) { 
      // Stores the itemView in a public final member variable that can be used 
      // to access the context from any ViewHolder instance. 
      super(itemView); 

      nameTextView = (TextView) itemView.findViewById(R.id.tv_title); 
      imageView = (ImageView) itemView.findViewById(R.id.iv_icon); 
     } 
    } 
} 
+0

Не могли бы вы дать код, который вы пробовали? –

+0

Показать код и то, что вы пробовали до сих пор. – Mangesh

+0

Вы использовали scrollview в любом макете? –

ответ

0

я получил ответ .. я просто заменить следующий (@ ScreenSlidePageFragment.java)

rootView.findViewById(R.id.my_recycler_view); 

спасибо.