这是我的方案
我引用此link
我有一个包含FeedFragment的导航抽屉。而FeeedFragment有回收利用。
我的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;
}
}
答案 0 :(得分:4)
这应该有效
FeedDialogFragment newFragment = new FeedDialogFragment();
newFragment.show(((MainActivity)context).getSupportFragmentManager(), "tag");
答案 1 :(得分:2)
我的一位同事找到了这个解决方案感谢Rahul。
FeedDialogFragment newFragment = FeedDialogFragment.newInstance();
newFragment.show(((AppCompatActivity)context).getSupportFragmentManager(),"Title");
FeedDialogFragment newFragment = FeedDialogFragment.newInstance();
newFragment.show(((MainActivity)context).getSupportFragmentManager(),"Title");
在上面的代码中,MainActivity是我的父活动,其中包含我的片段。这两种解决方案都适用于我。
因为Fragment不能有活动上下文。