用意向回收器替换烤面包

时间:2016-07-16 16:17:17

标签: android android-intent android-recyclerview

我在网上尝试了很多方法但仍无法做到。 如何将其替换为意图活动? 这是我的主要活动

  public class MainActivity extends AppCompatActivity {
private List<firstpageitem> firstpageitemList = new ArrayList<>();
private RecyclerView recyclerView;
private firstpageadapter mAdapter;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    recyclerView = (RecyclerView) findViewById(R.id.recycler_view);

    mAdapter = new firstpageadapter(firstpageitemList);
    RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(getApplicationContext());
    recyclerView.setLayoutManager(mLayoutManager);
    recyclerView.setItemAnimator(new DefaultItemAnimator());
    recyclerView.addItemDecoration(new DividerItemDecoration(this, LinearLayoutManager.VERTICAL));
    recyclerView.setAdapter(mAdapter);
    recyclerView.addOnItemTouchListener(new RecyclerTouchListener(getApplicationContext(), recyclerView, new ClickListener() {
        @Override
        public void onClick(View view, int position) {
            firstpageitem firstpageitem = firstpageitemList.get(position);
            Intent intent = new Intent(getApplicationContext(), Animals.class);
            startActivity(intent);
        }

        @Override
        public void onLongClick(View view, int position) {

        }
    }));

    preparefirstpageitemData();
}

private void preparefirstpageitemData() {
    firstpageitem firstpageitem = new firstpageitem(getString(R.string.firstanimals), "ʕ •ᴥ•ʔ");
    firstpageitemList.add(firstpageitem);

    firstpageitem = new firstpageitem(getString(R.string.firstflip), "(╯°□°)╯︵ ┻━┻");
    firstpageitemList.add(firstpageitem);

    firstpageitem = new firstpageitem(getString(R.string.firsthappy), "(ノ^_^)ノ");
    firstpageitemList.add(firstpageitem);

    firstpageitem = new firstpageitem(getString(R.string.firstmisc), "ⅽ[ː̠̈ː̠̈ː̠̈] ͌");
    firstpageitemList.add(firstpageitem);

    firstpageitem = new firstpageitem(getString(R.string.firstrage), "(╬゚д゚)");
    firstpageitemList.add(firstpageitem);

    firstpageitem = new firstpageitem(getString(R.string.firstsad), "。゚ヽ(゚´Д`)ノ゚。");
    firstpageitemList.add(firstpageitem);

    mAdapter.notifyDataSetChanged();
}

public interface ClickListener {
    void onClick(View view, int position);

    void onLongClick(View view, int position);
}

public static class RecyclerTouchListener implements RecyclerView.OnItemTouchListener {

    private GestureDetector gestureDetector;
    private MainActivity.ClickListener clickListener;

    public RecyclerTouchListener(Context context, final RecyclerView recyclerView, final MainActivity.ClickListener clickListener) {
        this.clickListener = clickListener;
        gestureDetector = new GestureDetector(context, new GestureDetector.SimpleOnGestureListener() {
            @Override
            public boolean onSingleTapUp(MotionEvent e) {
                return true;
            }

            @Override
            public void onLongPress(MotionEvent e) {
                View child = recyclerView.findChildViewUnder(e.getX(), e.getY());
                if (child != null && clickListener != null) {
                    clickListener.onLongClick(child, recyclerView.getChildPosition(child));
                }
            }
        });
    }

    @Override
    public boolean onInterceptTouchEvent(RecyclerView rv, MotionEvent e) {

        View child = rv.findChildViewUnder(e.getX(), e.getY());
        if (child != null && clickListener != null && gestureDetector.onTouchEvent(e)) {
            clickListener.onClick(child, rv.getChildPosition(child));
        }
        return false;
    }

    @Override
    public void onTouchEvent(RecyclerView rv, MotionEvent e) {
    }

    @Override
    public void onRequestDisallowInterceptTouchEvent(boolean disallowIntercept) {


    }

}

} 这是我的适配器

public class firstpageadapter扩展了RecyclerView.Adapter {

    private List<firstpageitem> firstpageitemList;




    public firstpageadapter(List<firstpageitem> firstpageitemList) {
        this.firstpageitemList = firstpageitemList;

    }

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

        return new MyViewHolder(itemView);
    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, int position) {
        firstpageitem firstpageitem = firstpageitemList.get(position);
        holder.title.setText(firstpageitem.getTitle());
        holder.emoticons.setText(firstpageitem.getEmoticons());


    }

@Override
public int getItemCount() {

    return firstpageitemList.size();
}

public class MyViewHolder extends RecyclerView.ViewHolder  {
    public TextView title, emoticons;

    private final Context context;

    public MyViewHolder(View view) {
        super(view);
        context = itemView.getContext();
        title = (TextView) view.findViewById(R.id.title);
        emoticons = (TextView) view.findViewById(R.id.emoticons);




    }


}

}

firstpage.java

public class firstpageitem {     私人字符串标题,表情符号;

public firstpageitem() {
}

public firstpageitem(String title, String emoticons) {
    this.title = title;
    this.emoticons = emoticons;
}

public String getTitle() {
    return title;
}

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

public String getEmoticons() {
    return emoticons;
}

public void setEmoticons(String emoticons) {
    this.emoticons = emoticons;
}

}

1 个答案:

答案 0 :(得分:1)

代替您正在制作的祝酒词,您需要创建一个Intent并使用它来开始您的新活动。

取代:

Toast.makeText(getApplicationContext(), firstpageitem.getTitle() + " is selected!", Toast.LENGTH_SHORT).show();

使用:

Intent intent = new Intent(getApplicationContext(), NewActivityName.class);
startActivity(intent);

NewActivityName是您将要开始的新活动类的名称。它也必须在您的AndroidManifest.xml中声明。如果您想将数据传递给新活动,您可以使用额外内容,这些内容也包含在我上面包含的意图链接中。

修改 要让每个回收站视图项打开不同的活动,您需要在firstpageitem中包含一些有关应该打开的活动的数据。以下是您将如何实现这一目标。

1)将firstpageitem更改为包含应该启动的类的字段(actionClass):

public class firstpageitem { 

  private String title, emoticons;
  private Class actionClass; //add this line

  public firstpageitem() {}

  public firstpageitem(String title, String emoticons, Class actionClass) {
      this.title = title;
      this.emoticons = emoticons;
      this.actionClass = actionClass;
  }

  public Class getActionClass() {
      return actionClass;
  }

  //.... keep you other getters and setters, I omited them for conciseness. 
  //....
}

2)填充actionClass方法中的preparefirstpageitemData字段。您应该将类​​用于您已创建的其他活动(例如:AnimalActivityOtherActivity):

private void preparefirstpageitemData() {
    // pass in that new constructor argument, the class the item should start!
    firstpageitem firstpageitem = new firstpageitem(getString(R.string.firstanimals), "ʕ •ᴥ•ʔ", AnimalActivity.class);
    firstpageitemList.add(firstpageitem);

    firstpageitem = new firstpageitem(getString(R.string.firstflip), "(╯°□°)╯︵ ┻━┻", OtherActivity.class);
    firstpageitemList.add(firstpageitem);

    // And the rest of the items too, I omitted them for conciseness.
}

3)最后,在你的onClick方法中,你需要获取被点击的项目的actionClass并在你的意图中使用它:

   @Override
    public void onClick(View view, int position) {
        firstpageitem firstpageitem = firstpageitemList.get(position);
        // get the action class here!
        Intent intent = new Intent(getApplicationContext(), firstPageItem.getActionClass());
        startActivity(intent);
    }

PS:我建议您在java和PascalCasing中使用camelCasing作为类名的变量。我有一个java style guide,如果您感兴趣,可以查看它,如果您的代码遵循这些做法,它可以让您更轻松地维护代码并获得其他人的帮助。