Android-处理活动中的片段onClick [英] Android - handle fragment onClick from activity

查看:107
本文介绍了Android-处理活动中的片段onClick的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个片段类,其中包含一些字段.

I have a fragment class, that contains some field.

public class ReviewFragment extends Fragment {

    //...
    private String paramText;
    public String getParamText() { return paramText; }
    //...
}

在活动中,我添加了该片段的多个实例,并设置了该字段值.另外,我将每个实例都放在List中,并设置fragment标签为"fragment0","fragment1"等.

From activity I add several instances of this fragment, and set this field value. Also I put each instance in List and set fragment tag = "fragment0", "fragment1" etc.

public class ReviewActivity extends FragmentActivity {

    private List<ReviewFragment> reviewFragments = new ArrayList<>();

    //...

    @Override
    protected void onCreate(Bundle savedInstanceState) {

          //...

          int i = 0;
          for (Comment comment : comments) {
                ReviewFragment reviewFragment = ReviewFragment.newInstance(comment.getAuthor(), comment.getText(), comment.getRate());
                reviewFragments.add(reviewFragment);
                transaction.add(ll.getId(), reviewFragment, "review" + i);
                i++;
          }
    }

我需要处理对ReviewFragment实例的每个实例的单击.因此,在Android Studio设计器中,我将以下方法附加到ReviewFragment根布局的onClick上.

I need to handle the click on the each instance of ReviewFragment instance. So in Android Studio designer I attach the following method to onClick of the root layout of ReviewFragment.

    public void reviewClick(View view) {
        Intent intent = new Intent(this, FullReviewActivity.class);
        String fragmentTag = view.getTag().toString();
        //...
        startActivity(intent);
    }

我正在尝试获取片段标签以在我的reviewFragments列表中找到该片段.问题是view.getTag()返回null,而不是"reviewN".我试图在ReviewFragment类中编写相同的方法,但是Android Studio不允许我将其附加到onClick.

I'm trying to get fragment tag to find the fragment in my reviewFragments List. The problem is that view.getTag() return null, not the "reviewN". I tried to write the same method in ReviewFragment class, but Android Studio didn't allow me to attach it to onClick.

所以问题是-在onClick处理方法中,我需要同时获取活动和片段.我该怎么办?

So problem is - in onClick handle method I need to get both activity and fragment. How can I do it?

推荐答案

如果您需要一堆 Fragments .也许在这种情况下,最好使用 ListView (或其他任何类型,例如 RecyclerView ).您可以轻松地监听click事件,并在列表项数不限的情况下执行任何操作.

In case you need to have a bunch of Fragments. Maybe in this case it is better to use ListView (or any other type of it, for example RecyclerView). You can easily listen for click event and do whatever you want having number of list item.

如果您确实需要使用片段.您可以采用不同的方法.
首先,在您的 reviewClick(View view)中,您获得的不是片段标签,而是视图标签.为什么将 View 作为参数?
如果需要片段在 Activity 中使用回调方法,则可以遵循最常用的方法.
这里的问题是您的 Framgent 可能包含不同的视图,并且如果不使用叠加布局,则无法直接获得整个片段的onClick,但是在这种情况下,您还必须处理布局onclick事件.>正如我已经提到的,可能的解决方案是使用回调方法.

If you really need to use Fragments. You can follow different ways.
Firstly in your reviewClick(View view) you are getting not fragment tag, but view tag. Why you are having View as an argument ?
You can follow most common used approach in case you need fragments use callback method in your Activity.
The problem here that your Framgent may contain different views and you cannot get onClick directly on whole fragment, if you are not using overlay layout, but in this case you also have to handle layout onclick event.
Possible solution as I have already mentioned is to use callback method.

以下是实现此步骤的几个步骤:

Here are several steps to implement this :

在片段中创建内部接口,例如

Create inner interface in your fragment,for example

 public interface       OnReviewSelectedListener {
        public void onReviewSelected(int position);  }

您的活动现在必须实现此接口

Your activtiy now have to implement this interface

public static class MainActivity extends Activity
        implements ReviewFragment.OnReviewSelectedListener

您可以在

 public void onReviewSelected(int position) {
          if(position==YOUR_TAG) {

          }
    }

onAttach 方法的 ReviewFragment 中,您必须将活动投射到界面上.

And in ReviewFragment in onAttach method you have to cast activity to your interface.

   @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);

        // This makes sure that the container activity has implemented
        // the callback interface. If not, it throws an exception
        try {
            mCallback = (OnReviewSelectedListener) activity;
        } catch (ClassCastException e) {
            throw new ClassCastException(activity.toString()
                    + " must implement OnReviewSelectedListener");
        }
    }

如果您真的想将片段与标签一起使用,则可以在片段中执行以下操作.

And if you really wanna to use fragment with tags you can do following in your fragment.

public void onCommentClick() {
      // do some stuff
     mCallback.onReviewSelected(getFramgentTag());
}

这篇关于Android-处理活动中的片段onClick的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆