免责声明:我是一个菜鸟
这里的情况是我有一个主要活动作为我的三个片段的调解者,并且在三个片段中有各自的回收者视图。
我的问题是我需要设置主要的三个frags的所有回收站视图的适配器,它不能由intent / bundle完成,因为它们全部同时运行而我似乎无法从主要活动中调用回收者对3个碎片的看法。
主要示例
public class classList_index extends AppCompatActivity {
private float c_qGrade;
private List<QuarterlyGrade_Config> TransGrade;
private List<tracksettingEquiv_Config> equiv;
private List<maxGrade_Config> maxgrade;
private List<batchList_config> studentList;
f_ww_Adapter wwAdapter;
f_pt_Adapter ptAdapter;
f_qe_Adapter qeAdapter;
FragmentPagerAdapter adapterViewPager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_class_list_index);
ViewPager vpPager = (ViewPager) findViewById(R.id.vpPager);
adapterViewPager = new MyPagerAdapter(getSupportFragmentManager());
vpPager.setAdapter(adapterViewPager);
vpPager.setCurrentItem(0);
populateList();
}
private void populateList()
{
studentList = new ArrayList<>();
final String loadURL = APP.DOMAIN+"/selectStudents.php";
RequestQueue reqQueue = Volley.newRequestQueue(classList_index.this);
StringRequest loadRequest = new StringRequest(Request.Method.POST, loadURL, new Response.Listener<String>() {
@Override
public void onResponse(String response){
try
{
JSONObject CLASS_LIST_JSON = new JSONObject(response);
JSONArray STUDENT_LIST_JARRAY = CLASS_LIST_JSON.getJSONArray("student_list");
for(int students = 0; students < STUDENT_LIST_JARRAY.length(); students++)
{
JSONObject p_JSONObject = STUDENT_LIST_JARRAY.getJSONObject(students);
final String p_studentID = p_JSONObject.getString("USERID");
final String p_lastName = p_JSONObject.getString("LASTNAME");
final String p_firstName = p_JSONObject.getString("FIRSTNAME");
final String p_middleName = p_JSONObject.getString("MIDDLENAME");
final String p_gender = p_JSONObject.getString("GENDER");
final String p_ww1 = p_JSONObject.getString("WW1");
final String p_ww2 = p_JSONObject.getString("WW2");
final String p_ww3 = p_JSONObject.getString("WW3");
final String p_ww4 = p_JSONObject.getString("WW4");
final String p_ww5 = p_JSONObject.getString("WW5");
final String p_ww6 = p_JSONObject.getString("WW6");
final String p_ww7 = p_JSONObject.getString("WW7");
final String p_ww8 = p_JSONObject.getString("WW8");
final String p_ww9 = p_JSONObject.getString("WW9");
final String p_ww10 = p_JSONObject.getString("WW10");
final String p_pt1 = p_JSONObject.getString("PT1");
final String p_pt2 = p_JSONObject.getString("PT2");
final String p_pt3 = p_JSONObject.getString("PT3");
final String p_pt4 = p_JSONObject.getString("PT4");
final String p_pt5 = p_JSONObject.getString("PT5");
final String p_pt6 = p_JSONObject.getString("PT6");
final String p_pt7 = p_JSONObject.getString("PT7");
final String p_pt8 = p_JSONObject.getString("PT8");
final String p_pt9 = p_JSONObject.getString("PT9");
final String p_pt10 = p_JSONObject.getString("PT10");
final String p_qe = p_JSONObject.getString("QE1");
batchList_config studentConfig = new batchList_config(p_studentID,p_firstName,p_lastName,p_middleName,p_gender,p_ww1,p_ww2,p_ww3,p_ww4,p_ww5,p_ww6,p_ww7,p_ww8,p_ww9,p_ww10,p_pt1,p_pt2,p_pt3,p_pt4,p_pt5,p_pt6,p_pt7,p_pt8,p_pt9,p_pt10,p_qe);
studentList.add(studentConfig);
}
wwAdapter = new f_ww_Adapter(classList_index.this, studentList);
ptAdapter = new f_pt_Adapter(classList_index.this, studentList);
qeAdapter = new f_qe_Adapter(classList_index.this, studentList);
//where the adapter of recycler views should all be set
}
catch (JSONException e) {
volleyErrorClass.catchInvalidResponse(e.toString(), classList_index.this);
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
volleyErrorClass.catchError(error, classList_index.this);
error.printStackTrace();
}
});
reqQueue.add(loadRequest);
}
public static class MyPagerAdapter extends FragmentPagerAdapter
{
private static int NUM_ITEMS = 3;
public MyPagerAdapter(FragmentManager fragmentManager) {
super(fragmentManager);
}
// Returns total number of pages
@Override
public int getCount() {
return NUM_ITEMS;
}
// Returns the fragment to display for that page
@Override
public Fragment getItem(int position) {
switch (position) {
case 0:
return f_wworks.newInstance();
case 1:
return f_ptasks.newInstance();
case 2:
return f_qexams.newInstance();
default:
return null;
}
}
// Returns the page title for the top indicator
@Override
public CharSequence getPageTitle(int position) {
switch (position)
{
case 0:
return "Written\nWorks";
case 1:
return "Performance\nTasks";
case 2:
return "Quarterly\nAssessment";
default:
return null;
}
}
}
片段示例
public class f_wworks extends Fragment {
private static RecyclerView rv_ww;
private OnFragmentInteractionListener mListener;
public f_wworks() {
}
public static f_wworks newInstance() {
f_wworks fragment = new f_wworks();
return fragment;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View holder = inflater.inflate(R.layout.fragment_wworks, container, false);
rv_ww = (RecyclerView) holder.findViewById(R.id.rv_wworks);
rv_ww.setLayoutManager(new LinearLayoutManager(getActivity()));
rv_ww.setHasFixedSize(true);
return holder;
}
// 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;
}
public interface OnFragmentInteractionListener {
// TODO: Update argument type and name
void onFragmentInteraction(Uri uri);
}
答案 0 :(得分:0)
您可以使用#=> permissions.rb
module Permissions
...
def self.method #=> method name instead of the method
#=> code staff here
end
or
def method #=> method name instead of the method
#=> code staff here
end
...
end
中的fragment
实例并使用它来调用Main
中的公共方法,如
主要强>
Fragment
现在你的片段
<强> f_wworks 强>
//declare globally
f_wworks fw;
f_ptasks fp;
f_qexams fq;
@Override
public Fragment getItem(int position) {
switch (position) {
case 0:
fw = f_wworks.newInstance();
return fw;
case 1:
fp = f_ptasks.newInstance();
return fp;
case 2:
fq = f_qexams.newInstance();
return fq;
default:
return null;
}
}
private void populateList()
{
//Once you created adapter
fw.setDataToAdapter(wwAdapter); //do this to other fragments
}