我正在一个Android项目中,在一个特定的布局中有一个选项卡式视图,在一个选项卡式视图中。所以我在那种情况下使用了布局片段。其中一个片段中有一个ListView
。 listview
基本上是一个时间轴视图,它将显示来自特定数据集的数据。正在成功检索数据,但是SetAdapter
函数不起作用。
这是选项卡式活动的主要View类,
import android.net.Uri;
import android.support.design.widget.TabLayout;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import com.impresscapital.iclapp.util.AboutICLPagerAdapter;
public class AboutICLMain extends AppCompatActivity
implements AboutICL.OnFragmentInteractionListener,
WalkThroughAchievements.OnFragmentInteractionListener,
DirectorManagement.OnFragmentInteractionListener{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_about_iclmain);
generateUI();
}
public void generateUI(){
TabLayout tabLayout = (TabLayout) findViewById(R.id.about_icl_page_tabs);
tabLayout.addTab(tabLayout.newTab().setText("About ICL"));
tabLayout.addTab(tabLayout.newTab().setText("Achievements"));
tabLayout.addTab(tabLayout.newTab().setText("Directors and Management"));
tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);
final ViewPager viewPager = (ViewPager) findViewById(R.id.pager);
final AboutICLPagerAdapter adapter = new AboutICLPagerAdapter
(getSupportFragmentManager(), tabLayout.getTabCount());
viewPager.setAdapter(adapter);
viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
@Override
public void onTabSelected(TabLayout.Tab tab) {
viewPager.setCurrentItem(tab.getPosition());
}
@Override
public void onTabUnselected(TabLayout.Tab tab) {
}
@Override
public void onTabReselected(TabLayout.Tab tab) {
}
});
}
@Override
public void onFragmentInteraction(Uri uri) {
}
}
现在要碎片活动
import android.content.Context;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import com.impresscapital.iclapp.data.ICLAchievementsData;
import com.impresscapital.iclapp.database.AppDatabase;
import com.impresscapital.iclapp.database.AppExecutors;
import com.impresscapital.iclapp.dbobj.ICLAchievements;
import com.impresscapital.iclapp.util.GetICLAchievements;
import com.impresscapital.iclapp.util.TimeLineAdapter;
import org.qap.ctimelineview.TimelineRow;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.zip.Inflater;
/**
* A simple {@link Fragment} subclass.
* Activities that contain this fragment must implement the
* {@link WalkThroughAchievements.OnFragmentInteractionListener} interface
* to handle interaction events.
* Use the {@link WalkThroughAchievements#newInstance} factory method to
* create an instance of this fragment.
*/
public class WalkThroughAchievements extends Fragment {
// TODO: Rename parameter arguments, choose names that match
// the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
private static final String ARG_PARAM1 = "param1";
private static final String ARG_PARAM2 = "param2";
private Context context;
// TODO: Rename and change types of parameters
private String mParam1;
private String mParam2;
private ListView achievementListView;
private AppDatabase mDb;
private ArrayList<ICLAchievements> achievements=new ArrayList<ICLAchievements>();
private ArrayList<TimelineRow> timelineRows=new ArrayList<TimelineRow>();
private OnFragmentInteractionListener mListener;
public WalkThroughAchievements() {
// Required empty public constructor
}
/**
* Use this factory method to create a new instance of
* this fragment using the provided parameters.
*
* @param param1 Parameter 1.
* @param param2 Parameter 2.
* @return A new instance of fragment WalkThroughAchievements.
*/
// TODO: Rename and change types and number of parameters
public static WalkThroughAchievements newInstance(String param1, String param2) {
WalkThroughAchievements fragment = new WalkThroughAchievements();
Bundle args = new Bundle();
args.putString(ARG_PARAM1, param1);
args.putString(ARG_PARAM2, param2);
fragment.setArguments(args);
return fragment;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
mParam1 = getArguments().getString(ARG_PARAM1);
mParam2 = getArguments().getString(ARG_PARAM2);
}
context=getContext();
}
private void generateUI(){
final TimeLineAdapter adapter=new TimeLineAdapter();
ICLAchievementsData data=new ICLAchievementsData();
achievements=data.getAllData();
timelineRows=adapter.generateTimeLine(achievements);
ArrayAdapter<TimelineRow> achievementAdapter=new
ArrayAdapter<TimelineRow>(
context.getApplicationContext()
,R.layout.fragment_walk_through_achievements,
timelineRows);
achievementListView.setAdapter(achievementAdapter);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
LayoutInflater layoutInflater=getActivity().getLayoutInflater();
View iclAchievementView=layoutInflater.inflate(
R.layout.fragment_walk_through_achievements,
container,false
);
achievementListView=iclAchievementView.findViewById(R.id.timeline_icl_achievements);
generateUI();
return inflater.inflate(R.layout.fragment_walk_through_achievements, container, false);
}
// TODO: Rename method, update argument and hook method into UI event
public void onButtonPressed(Uri uri) {
if (mListener != null) {
mListener.onFragmentInteraction(uri);
}
}
@Override
public void onAttach(Context context) {
super.onAttach(context);
if (context instanceof OnFragmentInteractionListener) {
mListener = (OnFragmentInteractionListener) context;
} else {
throw new RuntimeException(context.toString()
+ " must implement OnFragmentInteractionListener");
}
}
@Override
public void onDetach() {
super.onDetach();
mListener = null;
}
/**
* This interface must be implemented by activities that contain this
* fragment to allow an interaction in this fragment to be communicated
* to the activity and potentially other fragments contained in that
* activity.
* <p>
* See the Android Training lesson <a href=
* "http://developer.android.com/training/basics/fragments/communicating.html"
* >Communicating with Other Fragments</a> for more information.
*/
public interface OnFragmentInteractionListener {
// TODO: Update argument type and name
void onFragmentInteraction(Uri uri);
}
public ArrayList<ICLAchievements> getAllAchievements(){
GetICLAchievements getICLAchievements=new GetICLAchievements();
return getICLAchievements.getAllAchievements(context);
}
}
和片段布局
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".WalkThroughAchievements">
<!-- TODO: Update blank fragment layout -->
<ListView
android:id="@+id/timeline_icl_achievements"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:divider="@null"
android:dividerHeight="0dp" />
</FrameLayout>
我的适配器代码如下:
package com.impresscapital.iclapp.util;
import android.graphics.Color;
import com.impresscapital.iclapp.R;
import com.impresscapital.iclapp.dbobj.ICLAchievements;
import org.qap.ctimelineview.TimelineRow;
import java.util.ArrayList;
public class TimeLineAdapter {
private ArrayList<TimelineRow> timelineRowsList=new ArrayList<TimelineRow>();
public ArrayList<TimelineRow> generateTimeLine(ArrayList<ICLAchievements> achievements){
int rowId=0;
for(ICLAchievements achievement:achievements){
TimelineRow achievementRow=new TimelineRow(rowId++);
achievementRow.setTitle(achievement.getAchievementDate());
achievementRow.setDescription(achievement.getAchievementText());
achievementRow.setBellowLineColor(Color.argb(255, 0, 0, 0));
achievementRow.setBellowLineSize(1);
achievementRow.setImageSize(10);
achievementRow.setBackgroundSize(10);
achievementRow.setTitleColor(Color.argb(255, 0, 0, 0));
achievementRow.setDescriptionColor(Color.argb(255, 0, 0, 0));
timelineRowsList.add(achievementRow);
}
return timelineRowsList;
}
}