Show FragmentDialog in RecyclerView adapter

青春壹個敷衍的年華 提交于 2019-12-24 06:49:45

问题


Here is my scenario

I refer this link

I have a Navigation Drawer it contain FeedFragment. And FeeedFragment have recyclerview.

And my onClick is in RecyclerView adapter so i wanna to open FragmentDialog in this onclick method so please help me..

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

In this adapter i got error in this line newFragment.show(((Activity)context).getSupportFragmentManager()); it says getSupportFragmentManager() can not resolve

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

}

回答1:


This should work

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



回答2:


one of my SO mate found this solution Thanks to Rahul.

FeedDialogFragment newFragment = FeedDialogFragment.newInstance();
                    newFragment.show(((AppCompatActivity)context).getSupportFragmentManager(),"Title");

OR

 FeedDialogFragment newFragment = FeedDialogFragment.newInstance();
                        newFragment.show(((MainActivity)context).getSupportFragmentManager(),"Title");

In above code MainActivity is my parent activity which containing my fragment. This both solution work for me.

Because Fragment can not have a activity context.



来源:https://stackoverflow.com/questions/38802070/show-fragmentdialog-in-recyclerview-adapter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!