这是我在SOverflow中的第一个询问,希望您能尽快帮助我,
我试图从自己的OnItemClickListener替换一个Fragment,以将第二个Fragment放入相同的FrameLayout中,但是当我从列表中单击Item时,屏幕变成空白,并且Log不显示任何Exception或其他任何内容。 顺便说一句,对不起,如果某些代码是用西班牙语单词写的,或者我犯了一些语法错误。
public class MainActivity extends FragmentActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
SportsFragment frgInicial = new SportsFragment();
getSupportFragmentManager().beginTransaction().add(R.id.fragmentContainer,frgInicial,"Sports").commit();
}
}
public class SportsFragment extends Fragment {
public ApiClient clientCalls;
public ListView listViewSports;
public SportsFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View root = inflater.inflate(R.layout.fragment_sports, container, false);
listViewSports = (ListView) root.findViewById(R.id.sportsList);
clientCalls = new ApiClient(this.getContext());
List<Sport> sportList = recuperarDatosApi();
SportsArrayAdapter saa = new SportsArrayAdapter(getActivity(), sportList);
listViewSports.setOnItemClickListener(new AdapterView.OnItemClickListener(){
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Sport selectedSport = (Sport)listViewSports.getItemAtPosition(position);
Bundle args = new Bundle();
byte[] bytes = null;
try{
ByteArrayOutputStream bos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(bos);
oos.writeObject(recuperarDatosLigas(selectedSport.getStrSport()));
bytes = bos.toByteArray();
oos.close();
bos.close();
}catch(IOException ioe){
}
args.putByteArray("LeagueList",bytes);
CountryLeagueFragment fragment = new CountryLeagueFragment();
fragment.setArguments(args);
FragmentManager fragmentManager = getActivity().getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.fragmentContainer,fragment);
fragmentTransaction.commit();
}
});
listViewSports.setAdapter(saa);
return root;
}
public List<Sport> recuperarDatosApi(){
return clientCalls.getAllSports();
}
public List<CountryLeague> recuperarDatosLigas(String sport){
return clientCalls.getAllLeaguesFromSport(sport);
}
}
两种方法的缺点都是要从API调用中获取一些信息
public class CountryLeagueFragment extends Fragment {
public ListView listViewCountryLeague;
public CountryLeagueFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View root = inflater.inflate(R.layout.fragment_country_league, container, false);
listViewCountryLeague = (ListView) root.findViewById(R.id.countryleaguelist);
Bundle args = getArguments();
List<CountryLeague> listaLigas = null;
try{
ByteArrayInputStream bis = new ByteArrayInputStream(args.getByteArray("LeagueList"));
ObjectInputStream ois = new ObjectInputStream(bis);
listaLigas = (List<CountryLeague>) ois.readObject();
}catch(IOException ioe){
ioe.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
CountryLeagueArrayAdapter claa = new CountryLeagueArrayAdapter(getActivity(),listaLigas);
listViewCountryLeague.setAdapter(claa);
return root;
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<FrameLayout
android:id="@+id/fragmentContainer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
我的自定义列表容器
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<ImageView
android:id="@+id/icon"
android:layout_width="115dp"
android:layout_height="122dp"
android:padding="5dp" />
<LinearLayout
android:layout_width="296dp"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginTop="5dp"
android:padding="2dp"
android:text="Medium Text"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#4d4d4d"
android:textStyle="bold" />
<TextView
android:id="@+id/subtitle"
android:layout_width="284dp"
android:layout_height="89dp"
android:layout_marginLeft="10dp"
android:text="TextView" />
</LinearLayout>
</LinearLayout>
还有ArrayAdapter
public class SportsArrayAdapter extends ArrayAdapter<Sport> {
private final Context context;
private List<Sport> lista = null;
public SportsArrayAdapter(Context context, List<Sport> lista){
super(context, R.layout.custom_item_list,lista);
this.context = context;
this.lista = lista;
}
public View getView(int position, View view, ViewGroup parent){
LayoutInflater inflater = LayoutInflater.from(context);
View rowView = inflater.inflate(R.layout.custom_item_list,null,true);
TextView titleText = (TextView) rowView.findViewById(R.id.title);
ImageView imageView = (ImageView) rowView.findViewById(R.id.icon);
TextView subtitleText = (TextView) rowView.findViewById(R.id.subtitle);
titleText.setText(lista.get(position).getStrSport());
Picasso.get().load(lista.get(position).getStrSportThumb()).into(imageView);
subtitleText.setText(lista.get(position).getStrSportDescription());
return rowView;
}
}
public class CountryLeagueArrayAdapter extends ArrayAdapter<CountryLeague> {
private final Context context;
private List<CountryLeague> lista = null;
public CountryLeagueArrayAdapter(Context context, List<CountryLeague> lista) {
super(context, R.layout.custom_item_list);
this.context = context;
this.lista = lista;
}
public View getView(int position, View view, ViewGroup parent){
LayoutInflater inflater = LayoutInflater.from(context);
// context.getLayoutInflater();
View rowView = inflater.inflate(R.layout.custom_item_list,null,true);
TextView titleText = (TextView) rowView.findViewById(R.id.title);
ImageView imageView = (ImageView) rowView.findViewById(R.id.icon);
TextView subtitleText = (TextView) rowView.findViewById(R.id.subtitle);
titleText.setText(lista.get(position).getStrCountry());
Picasso.get().load(lista.get(position).getStrBadge()).into(imageView);
subtitleText.setText(lista.get(position).getStrLeague());
return rowView;
}
}
编辑:我看到了我的问题,在super()调用中我没有使用CountryLeagueArrayAdapter作为列表的第三个参数,希望它对某人有帮助!