android - 在 RecyclerView 适配器中显示 FragmentDialog

标签 android android-fragments fragment android-dialogfragment

这是我的场景

我引用这个link

我有一个抽屉导航,它包含 FeedFragment。而 FeeedFragment 有 recyclerview。

我的 onClick 在 RecyclerView 适配器中,所以我想在这个 onclick 方法中打开 FragmentDialog 所以请帮助我..

FeedFragment.java

    import android.support.v4.app.FragmentManager;
import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.bicubic.tennis.R;
import com.bicubic.tennis.adapter.RVFeedAdapter;
import com.bicubic.tennis.model.Feed;

import java.util.ArrayList;
import java.util.List;

public class FeedFragment extends android.support.v4.app.Fragment {

    private List<Feed> feedList;
    private RecyclerView rv;
    View rootView;

    public FeedFragment() {
        // Required empty public constructor
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

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

        rv=(RecyclerView)rootView.findViewById(R.id.rv);

        LinearLayoutManager llm = new LinearLayoutManager(getActivity());
        rv.setLayoutManager(llm);
        rv.setHasFixedSize(true);

        initializeData();

        initializeAdapter();


        return rootView;
    }

    private void initializeData() {

        feedList = new ArrayList<>();

        feedList.add(new Feed("Rio Olympics 2016: 24 hours in 24 pictures - Friday 5 August 2016"));
        feedList.add(new Feed("Rio 2016 Olympics: Integrity of Games in tatters as IOC clears more than two-thirds of Russia team"));
        feedList.add(new Feed("Story of the local hero coach going to Rio thanks to crowdfunding shows the real soul of Olympic Games"));
        feedList.add(new Feed("Comment: Run rates are up and crowds are down: there are many reasons four-day Test matches are a good idea"));
        feedList.add(new Feed("Tom Watson says Shami Chakrabarti peerage is a 'mistake' as he reveals he was not consulted on decision"));
        feedList.add(new Feed("Cargo plane crashes onto busy Italian road after overshooting runway"));
        feedList.add(new Feed("Rio 2016 Olympics: Integrity of Games in tatters as IOC clears more than two-thirds of Russia team"));
        feedList.add(new Feed("Rio 2016 Olympics: Integrity of Games in tatters as IOC clears more than two-thirds of Russia team"));
        feedList.add(new Feed("Rio 2016 Olympics: Integrity of Games in tatters as IOC clears more than two-thirds of Russia team"));
        feedList.add(new Feed("Rio 2016 Olympics: Integrity of Games in tatters as IOC clears more than two-thirds of Russia team"));
        feedList.add(new Feed("Rio 2016 Olympics: Integrity of Games in tatters as IOC clears more than two-thirds of Russia team"));
        feedList.add(new Feed("Rio 2016 Olympics: Integrity of Games in tatters as IOC clears more than two-thirds of Russia team"));
        feedList.add(new Feed("Rio 2016 Olympics: Integrity of Games in tatters as IOC clears more than two-thirds of Russia team"));
        feedList.add(new Feed("Rio 2016 Olympics: Integrity of Games in tatters as IOC clears more than two-thirds of Russia team"));
        feedList.add(new Feed("Rio 2016 Olympics: Integrity of Games in tatters as IOC clears more than two-thirds of Russia team"));
        feedList.add(new Feed("Rio 2016 Olympics: Integrity of Games in tatters as IOC clears more than two-thirds of Russia team"));
        feedList.add(new Feed("Rio 2016 Olympics: Integrity of Games in tatters as IOC clears more than two-thirds of Russia team"));
        feedList.add(new Feed("Rio 2016 Olympics: Integrity of Games in tatters as IOC clears more than two-thirds of Russia team"));

    }


    private void initializeAdapter(){
        RVFeedAdapter adapter = new RVFeedAdapter(feedList,getActivity());
        rv.setAdapter(adapter);
    }

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

    @Override
    public void onDetach() {
        super.onDetach();
    }
}

在这个适配器中我在这行 newFragment.show(((Activity)context).getSupportFragmentManager()); 它说 getSupportFragmentManager() 无法解析

RVFeedAdapter.java

    import android.app.Activity;
import android.app.Fragment;
import android.support.v7.app.AppCompatActivity;
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import android.view.View.OnClickListener;

import com.bicubic.tennis.R;
import com.bicubic.tennis.fragment.FeedDialogFragment;
import com.bicubic.tennis.model.Feed;

import java.util.List;

/**
 * Created by admin on 8/5/2016.
 */
public class RVFeedAdapter extends RecyclerView.Adapter<RVFeedAdapter.FeedHolder> {

    static Context context;
    List<Feed> feedList;
    // Allows to remember the last item shown on screen
    private int lastPosition = -1;

    public RVFeedAdapter(List<Feed> feedList, Context context) {
        this.feedList = feedList;
        context = context;
    }

    public static class FeedHolder extends RecyclerView.ViewHolder implements OnClickListener {

        ImageView img_main;
        TextView tv_title;
        Button bt_facebook, bt_twitter, bt_share, bt_comment;


        public FeedHolder(View itemView) {
            super(itemView);

            img_main = (ImageView) itemView.findViewById(R.id.img_main);
            tv_title = (TextView) itemView.findViewById(R.id.tv_title);
            bt_facebook = (Button) itemView.findViewById(R.id.bt_facebook);
            bt_twitter = (Button) itemView.findViewById(R.id.bt_twitter);
            bt_share = (Button) itemView.findViewById(R.id.bt_share);
            bt_comment = (Button) itemView.findViewById(R.id.bt_comment);

            img_main.setOnClickListener(this);
            bt_facebook.setOnClickListener(this);
            bt_twitter.setOnClickListener(this);
            bt_comment.setOnClickListener(this);
            bt_share.setOnClickListener(this);

        }


        @Override
        public void onClick(View v) {

            if (v.getId() == bt_comment.getId()) {

                Toast.makeText(v.getContext(), "Comment  ", Toast.LENGTH_SHORT).show();

            } else if (v.getId() == bt_facebook.getId()) {

                Toast.makeText(v.getContext(), "Facebook  ", Toast.LENGTH_SHORT).show();

            } else if (v.getId() == bt_twitter.getId()) {

                Toast.makeText(v.getContext(), "Twitter  ", Toast.LENGTH_SHORT).show();

            } else if (v.getId() == bt_share.getId()) {

               /* //Change part below with your code
                FragmentTransaction fragmentTransaction = callback.fragmentManager.beginTransaction();
                FeedDialogFragment fragment = new FeedDialogFragment();

                Bundle bundle = new Bundle();
                bundle.putInt("position", getAdapterPosition());
                fragment.setArguments(bundle);

                fragmentTransaction.replace(R.id.activity_log_reader_relativeLayout1, fragment);
                fragmentTransaction.addToBackStack(null);
                fragmentTransaction.commit();
                //---End of change*/
            } else {
                Toast.makeText(v.getContext(), "ROW PRESSED = " + String.valueOf(getAdapterPosition()), Toast.LENGTH_SHORT).show();
                Toast.makeText(v.getContext(), "share  ", Toast.LENGTH_SHORT).show();

                FeedDialogFragment newFragment = FeedDialogFragment.newInstance();
                newFragment.show(((Activity)context).getSupportFragmentManager());
            }
        }
    }

    @Override
    public void onAttachedToRecyclerView(RecyclerView recyclerView) {
        super.onAttachedToRecyclerView(recyclerView);
    }

    @Override
    public FeedHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.feed_row, parent, false);
        FeedHolder feedHolder = new FeedHolder(view);

        return feedHolder;
    }

    @Override
    public void onBindViewHolder(FeedHolder holder, int position) {

        holder.tv_title.setText(feedList.get(position).getTitle());


        // Here you apply the animation when the view is bound
        setAnimation(holder.img_main, position);
    }

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


    /**
     * Here is the key method to apply the animation
     */
    private void setAnimation(View viewToAnimate, int position) {
        // If the bound view wasn't previously displayed on screen, it's animated
        if (position > lastPosition) {
            Animation animation = AnimationUtils.loadAnimation(context, android.R.anim.slide_in_left);
            viewToAnimate.startAnimation(animation);
            lastPosition = position;
        }
    }


}

FeedDialogFragment.java

    import android.app.FragmentManager;
import android.support.v4.app.DialogFragment;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.bicubic.tennis.R;

/**
 * Created by admin on 8/5/2016.
 */
public class FeedDialogFragment extends DialogFragment {

    public static FeedDialogFragment newInstance() {
        return new FeedDialogFragment();
    }

    public FeedDialogFragment() {
        // Empty constructor required for DialogFragment
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.feed_dialog, container, false);
        getDialog().setTitle("Simple Dialog");
        return rootView;
    }

}

最佳答案

这应该可行

 FeedDialogFragment newFragment = new FeedDialogFragment();
 newFragment.show(((MainActivity)context).getSupportFragmentManager(), "tag");

关于android - 在 RecyclerView 适配器中显示 FragmentDialog,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38802070/

相关文章:

android - 如何通过 adb shell 运行 QPython

android - 从 Fragment 对象访问 FragmentActivity 中的元素

java - fragment 错误

fragment 示例中的 Android ListView

php - 在 php 中的 Android post 请求

android - Lint 提示没有翻译 gcm_defaultSenderId 字符串

android - 在 ListView 中选择 edittexts 文本。怎么办?我找不到

最适合初始化/加载数据的Android Fragment方法

java - 使用“查看寻呼机”在选项卡中未显示列表

android - 如何更改 fragment Kotlin