Android View寻呼机和RecycleView

时间:2016-03-21 06:23:35

标签: android android-recyclerview

我只想在视图寻呼机内使用循环视图显示列表。 (例如,就像android主页图标一样) 但是这里列表没有显示,没有查看寻呼机它可以工作。

请有人能告诉我问题出在哪里...... 谢谢..

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

1 个答案:

答案 0 :(得分:0)

我得到了答案.. 我只是替换以下(@ ScreenSlidePageFragment.java)

def extract_from_zipfile(inzipfile):
    txtfile = ""
    with zipfile.ZipFile(inzipfile, "r") as z:
        z.extractall(txtfile)
        names = z.namelist()    # <---
        if names:               # <--- To prevent IndexError for empty zip file.
            txtfile = names[-1] # <---
    return txtfile

谢谢。

相关问题