如何在可点击的项目上打开新活动?

时间:2017-03-22 21:41:23

标签: java android android-recyclerview

我是Java的初学者,我下载了一个网格模板供他使用。我的时间很短,因此无法从这一切中学到0.请帮我解释一下这个代码,如何创建具有白页的新活动的可点击项目。

主要活动

public class MainActivity extends AppCompatActivity {

private RecyclerView recyclerView;
private AlbumsAdapter adapter;
private List<Album> albumList;


@Override

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    initCollapsingToolbar();

    recyclerView = (RecyclerView) findViewById(R.id.recycler_view);

    albumList = new ArrayList<>();
    adapter = new AlbumsAdapter(this, albumList);

    RecyclerView.LayoutManager mLayoutManager = new GridLayoutManager(this, 2);
    recyclerView.setLayoutManager(mLayoutManager);
    recyclerView.addItemDecoration(new GridSpacingItemDecoration(2, dpToPx(10), true));
    recyclerView.setItemAnimator(new DefaultItemAnimator());
    recyclerView.setAdapter(adapter);

    prepareAlbums();

    try {
        Glide.with(this).load(R.drawable.bghedb).into((ImageView) findViewById(R.id.backdrop));
    } catch (Exception e) {
        e.printStackTrace();
    }
}

/**
 * Initializing collapsing toolbar
 * Will show and hide the toolbar title on scroll
 */
private void initCollapsingToolbar() {
    final CollapsingToolbarLayout collapsingToolbar =
            (CollapsingToolbarLayout) findViewById(R.id.collapsing_toolbar);
    collapsingToolbar.setTitle(" ");
    AppBarLayout appBarLayout = (AppBarLayout) findViewById(R.id.appbar);
    appBarLayout.setExpanded(true);

    // hiding & showing the title when toolbar expanded & collapsed
    appBarLayout.addOnOffsetChangedListener(new AppBarLayout.OnOffsetChangedListener() {
        boolean isShow = false;
        int scrollRange = -1;

        @Override
        public void onOffsetChanged(AppBarLayout appBarLayout, int verticalOffset) {
            if (scrollRange == -1) {
                scrollRange = appBarLayout.getTotalScrollRange();
            }
            if (scrollRange + verticalOffset == 0) {
                collapsingToolbar.setTitle(getString(R.string.app_name));
                isShow = true;
            } else if (isShow) {
                collapsingToolbar.setTitle(" ");
                isShow = false;
            }
        }
    });
}

/**
 * Adding few albums for testing
 */
private void prepareAlbums() {
    int[] covers = new int[]{
            R.drawable.foto1,
            R.drawable.foto2,
            R.drawable.foto3,
            R.drawable.foto4,
            R.drawable.foto5,
            R.drawable.foto6,
            R.drawable.foto7,
            R.drawable.foto8,
            R.drawable.foto9,
            R.drawable.foto10,
            };

    Album a = new Album("Start A Blog", 13, covers[0]);
    albumList.add(a);

    a = new Album("Start A YouTube Channel", 8, covers[1]);
    albumList.add(a);

    a = new Album("Sell Websites On Flippa", 11, covers[2]);
    albumList.add(a);

    a = new Album("Join Leapforce", 12, covers[3]);
    albumList.add(a);

    a = new Album("Use CashPirate App", 14, covers[4]);
    albumList.add(a);

    a = new Album("Use ReceiptHog App", 1, covers[5]);
    albumList.add(a);

    a = new Album("Join Micro Job Sites", 11, covers[6]);
    albumList.add(a);

    a = new Album("Do CPA Marketing", 14, covers[7]);
    albumList.add(a);

    a = new Album("Start Copywriting", 11, covers[8]);
    albumList.add(a);

    a = new Album("Develop Mobile Apps", 17, covers[9]);
    albumList.add(a);

    adapter.notifyDataSetChanged();
}


/**
 * RecyclerView item decoration - give equal margin around grid item
 */
public class GridSpacingItemDecoration extends RecyclerView.ItemDecoration {

    private int spanCount;
    private int spacing;
    private boolean includeEdge;

    public GridSpacingItemDecoration(int spanCount, int spacing, boolean includeEdge) {
        this.spanCount = spanCount;
        this.spacing = spacing;
        this.includeEdge = includeEdge;
    }

    @Override
    public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
        int position = parent.getChildAdapterPosition(view); // item position
        int column = position % spanCount; // item column

        if (includeEdge) {
            outRect.left = spacing - column * spacing / spanCount; // spacing - column * ((1f / spanCount) * spacing)
            outRect.right = (column + 1) * spacing / spanCount; // (column + 1) * ((1f / spanCount) * spacing)

            if (position < spanCount) { // top edge
                outRect.top = spacing;
            }
            outRect.bottom = spacing; // item bottom
        } else {
            outRect.left = column * spacing / spanCount; // column * ((1f / spanCount) * spacing)
            outRect.right = spacing - (column + 1) * spacing / spanCount; // spacing - (column + 1) * ((1f /    spanCount) * spacing)
            if (position >= spanCount) {
                outRect.top = spacing; // item top
            }
        }
    }
}

/**
 * Converting dp to pixel
 */
private int dpToPx(int dp) {
    Resources r = getResources();
    return Math.round(TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp, r.getDisplayMetrics()));


}

ALBUM ADAPTER

public class AlbumsAdapter extends RecyclerView.Adapter<AlbumsAdapter.MyViewHolder> {

private Context mContext;
private List<Album> albumList;

public class MyViewHolder extends RecyclerView.ViewHolder {
    public TextView title, count;
    public ImageView thumbnail, overflow;

    public MyViewHolder(View view) {
        super(view);
        title = (TextView) view.findViewById(R.id.title);
        count = (TextView) view.findViewById(R.id.count);
        thumbnail = (ImageView) view.findViewById(R.id.thumbnail);
        overflow = (ImageView) view.findViewById(R.id.overflow);
    }
}


public AlbumsAdapter(Context mContext, List<Album> albumList) {
    this.mContext = mContext;
    this.albumList = albumList;
}

@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View itemView = LayoutInflater.from(parent.getContext())
            .inflate(R.layout.album_card, parent, false);

    return new MyViewHolder(itemView);
}

@Override
public void onBindViewHolder(final MyViewHolder holder, int position) {
    Album album = albumList.get(position);
    holder.title.setText(album.getName());


    // loading album cover using Glide library
    Glide.with(mContext).load(album.getThumbnail()).into(holder.thumbnail);

    holder.overflow.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

        }
    });
}

/**
 * Showing popup menu when tapping on 3 dots
 */



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

ALBUM

public Album(String name, int numOfSongs, int thumbnail) {
    this.name = name;
    this.numOfSongs = numOfSongs;
    this.thumbnail = thumbnail;
}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public int getNumOfSongs() {
    return numOfSongs;
}

public void setNumOfSongs(int numOfSongs) {
    this.numOfSongs = numOfSongs;
}

public int getThumbnail() {
    return thumbnail;
}

public void setThumbnail(int thumbnail) {
    this.thumbnail = thumbnail;
}

有人能帮帮我吗?我是否需要将每个字符串href到新页面白色,我的问题是任何有空闲时间照顾我从0学习的人?谢谢。

1 个答案:

答案 0 :(得分:0)

只需添加这些行

即可
holder.overflow.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        Intent intent = new Intent(context, YourActivity.class).addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
        context.startActivity(intent);
    }
});