我有RecyclerView
可以模拟新闻源。我想,如果您点击RecyclerView
的某个项目,则该项目的文字会显示在另一个Fragment's
TextView
中。新闻源内的帖子文本保存在ArrayList
中。我已经尝试了好几个小时,将ArrayList
的值传递到应该显示的TextView
Fragment
,但我真的不喜欢&# 39;得到它。这是我的代码:
RecyclerView适配器
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.view.ContextMenu;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.TextView;
public class RvAdapterKlasseTalkFragment extends RecyclerView.Adapter<RvAdapterKlasseTalkFragment.ViewHolderKlasseTalkFragment>{
public static class ViewHolderKlasseTalkFragment extends RecyclerView.ViewHolder{
TextView userName;
TextView postText;
// IMAGE FÜR DEN POST FEHLT!!!!!!!!!
TextView zeit;
TextView anzahlKommentare;
TextView anzahlLikes;
ImageButton moreButton;
TextView loeschen;
public ViewHolderKlasseTalkFragment (View itemView){
super(itemView);
userName = (TextView) itemView.findViewById(R.id.userName);
postText = (TextView) itemView.findViewById(R.id.postText);
zeit = (TextView) itemView.findViewById(R.id.zeit);
anzahlKommentare = (TextView) itemView.findViewById(R.id.anzahlKommentare);
anzahlLikes = (TextView) itemView.findViewById(R.id.anzahlLikes);
moreButton = (ImageButton) itemView.findViewById(R.id.moreButton);
loeschen = (TextView) itemView.findViewById(R.id.loeschen);
}
}
@Override
public ViewHolderKlasseTalkFragment onCreateViewHolder(ViewGroup viewGroup, int viewType) {
View itemView1 = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.iten_layout_post, null);
return new ViewHolderKlasseTalkFragment(itemView1);
}
@Override
public void onBindViewHolder(final ViewHolderKlasseTalkFragment viewHolderKlasse,final int i) {
viewHolderKlasse.userName.setText(TalkscreenFragment.userName);
viewHolderKlasse.userName.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(v.getContext(), ProfilePopUp.class);
v.getContext().startActivity(intent);
}
});
viewHolderKlasse.postText.setText(TalkscreenFragment.postText.get(i));
viewHolderKlasse.zeit.setText(TalkscreenFragment.currentDateTimeString);
viewHolderKlasse.anzahlKommentare.setText(TalkscreenFragment.anzahlKommentare);
viewHolderKlasse.anzahlLikes.setText(TalkscreenFragment.anzahlLikes);
viewHolderKlasse.moreButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(v.getContext(), ContextMenuPopUp.class);
v.getContext().startActivity(intent);
}
});
}
@Override
public int getItemCount() {
return TalkscreenFragment.postText.size();
}
}
使用ArrayList和RecylcerView的活动
import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ImageButton;
import android.widget.TextView;
import java.text.DateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
public class Talkscreen extends AppCompatActivity {
public static LinearLayoutManager rvLayoutManagerTalkFragment;
public static RecyclerView recyclerViewTalkFragment;
public static RecyclerView.Adapter rvadapterTalkFragment;
public static ArrayList<String> postText;
public static String userName = "Benutzer 1";
public static String anzahlKommentare = "1.243";
public static String anzahlLikes = "15540";
public static String currentDateTimeString;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_talkscreen);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(getBaseContext(), PostScreen.class);
startActivity(intent);
}
});
TextView meineaktivitaeten = (TextView) findViewById(R.id.meineAktivitaeten);
meineaktivitaeten.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(getBaseContext(), MeineAktivitaeten.class);
startActivity(intent);
}
});
postText = new ArrayList<>();
postText.addAll(Arrays.asList("TEXT 1",
"TEXT 2",
"TEXT 3",
"TEXT 4"));
currentDateTimeString = DateFormat.getTimeInstance().format(new Date());
recyclerViewTalkFragment = (RecyclerView) findViewById(R.id.recycleviewTalkFragment);
rvLayoutManagerTalkFragment = new LinearLayoutManager(this);
recyclerViewTalkFragment.setLayoutManager(rvLayoutManagerTalkFragment);
rvadapterTalkFragment = new RvAdapterKlasseTalkFragment();
recyclerViewTalkFragment.setAdapter(rvadapterTalkFragment);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_talkscreen, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.einstellungen) {
startActivity(new Intent(this, Einstellungen.class));
}
if (id == R.id.information) {
startActivity(new Intent(this, Information.class));
}
return super.onOptionsItemSelected(item);
}
}
应该显示ArrayList值的片段
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
/**
* A simple {@link Fragment} subclass.
*/
public class MeinePosts extends Fragment {
public static MeinePosts newInstance(){
MeinePosts fragment = new MeinePosts();
return fragment;
}
public MeinePosts() {
}
TextView postedText;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
View rootView = inflater.inflate(R.layout.fragment_meine_posts, container, false);
postedText = (TextView) rootView.findViewById(R.id.postTextMeinePosts);
return rootView;
}
}
感谢任何帮助!
答案 0 :(得分:0)
in
片段:
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
/**
* A simple {@link Fragment} subclass.
*/
public class MeinePosts extends Fragment {
private ArrayList<String> fragmentList;
public static MeinePosts newInstance(){
MeinePosts fragment = new MeinePosts();
return fragment;
}
/*HERE YOU RECIVE PARAMETER ARRAYLIST*/
public MeinePosts(ArrayList<String> myList) {
/*Now from here you can set the arraylist
to other... it can be access from any method
if you set on class*/
fragmentList = myList;
}
TextView postedText;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
View rootView = inflater.inflate(R.layout.fragment_meine_posts, container, false);
postedText = (TextView) rootView.findViewById(R.id.postTextMeinePosts);
return rootView;
}
}
答案 1 :(得分:0)
Android中交互式通信的最佳方式是通过两个片段中的接口,将Activity作为中间人。 为此,您必须为要使其能够向活动发送信息的片段声明一个接口。
首先在Fragment RvAdapterKlasseTalkFragment中执行此操作: 公共类RvAdapterKlasseTalkFragment扩展了RecyclerView.Adapter {
// Declare this interface
public Interface AdapterInterface() {
public void sendValueToActivity(String value);
}
// Create an object to reference the activity that implements the previous interface (Talkscreen)
AdapterInterface activityConnection;
// in your onclick call the interface
@Override
public void onBindViewHolder(final ViewHolderKlasseTalkFragment viewHolderKlasse,final int i) {
viewHolderKlasse.userName.setText(TalkscreenFragment.userName);
viewHolderKlasse.userName.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
connectedActivity.sendValueToActivity(myvalue);
}
});
// here is where you link the interface to the activity and you can openly exchange information now
@Override
public void onAttach(Context context) {
super.onAttach(context);
try {
activityConnection = (AdapterInterface) context;
} catch (ClassCastException e) {
throw new ClassCastException(context.toString() + " must implement AdapterInterface");
}
}
}
在MeineFragment中设置一个方法,您可以从Activity直接引用此类的片段对象来调用该方法:
public class MeinePosts extends Fragment {
// add this method to your MeinePosts fragment
public void setTextViewValue(String value) {
postedText.setText(value);
}
}
最后在你的Activity中实现第一个片段中声明的接口:
public class Talkscreen extends AppCompatActivity implements MyInterface {
@Override
public void sendValueToActivity(String value) {
// here invoque a method in the second fragment for example
AdapterFragment.setTextViewValue(value);
}
}
如果您还需要将第二个片段的信息发送到第一个片段,您还需要实现相同的步骤(在第二个片段中声明接口,在活动中实现它,在片段中链接它,以及在activity在第一个片段中调用一个新方法来做你想做的事情
如果您仍需要更多信息,我在Github中有这个项目进行片段间通信。 https://github.com/isaacurbina/FragmentsExercise
我希望它有所帮助!亲切的问候!
答案 2 :(得分:0)
首先,我犯了一个可怕的错误。 MeinePost片段中初始化的TextView指的是RelativeLayout而不是TextView。
正在进行中我发现了一种实现我想要的相对简单的方法。它可能不是最好的解决方案,但它对我有用:
第1步:
RecyclerView适配器
viewHolderKlasse.postText.setText(Talkscreen.postText.get(i));
viewHolderKlasse.postText.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(v.getContext(), AngeklickterPost.class);
intent.putExtra("data", Talkscreen.postText.get(i)); //data is a string variable holding some value.
v.getContext().startActivity(intent);
}
});
第二步:我需要为我的意图创建一个新的活动
AngeklickterPost活动
package com.appmac.ron.gamestalk;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.widget.TextView;
public class AngeklickterPost extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_angeklickter_post);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setHomeButtonEnabled(true);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
String data = getIntent().getStringExtra("data");
TextView postTextPosted = (TextView) findViewById(R.id.postTextPosted);
postTextPosted.setText(data);
}
}
希望这篇文章对可能遇到同样问题的人有所帮助。如果有什么我可以做得更好,请评论!