如何在另一个类中以及在此页中调用该类之后移动诸如getData()之类的方法?
我尝试做某事,但是没用。
您能帮忙创建一个新班吗?
package it.example.app.fragment_2;
import android.content.Context;
import android.net.Uri;
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 android.widget.ProgressBar;
import android.widget.Toast;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonArrayRequest;
import com.android.volley.toolbox.Volley;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.ArrayList;
import java.util.List;
public class Tab1 extends Fragment implements RecyclerView.OnScrollChangeListener {
View v;
private Tab1.OnFragmentInteractionListener mListener;
private List<SuperHero> listSuperHeroes;
//Creating Views
private RecyclerView recyclerView;
private RecyclerView.LayoutManager layoutManager;
private RecyclerView.Adapter adapter;
//Volley Request Queue
private RequestQueue requestQueue;
//The request counter to send ?page=1, ?page=2 requests
private int requestCount = 1;
public Tab1() {
// Required empty public constructor
}
@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
v = inflater.inflate(R.layout.fragment_tab1, container, false);
recyclerView = (RecyclerView) v.findViewById(R.id.recyclerView);
recyclerView.setHasFixedSize(true);
listSuperHeroes = new ArrayList<>();
requestQueue = Volley.newRequestQueue(getContext());
getData();
adapter = new CardAdapter(listSuperHeroes, getContext());
layoutManager = new LinearLayoutManager(getActivity());
recyclerView.setLayoutManager(layoutManager);
recyclerView.setOnScrollChangeListener(this);
recyclerView.setAdapter(adapter);
return v;
}
//This integer will used to specify the page number for the request ?page = requestcount
//This method would return a JsonArrayRequest that will be added to the request queue
private JsonArrayRequest getDataFromServer(int requestCount) {
//Initializing ProgressBar
//final ProgressBar progressBar = (ProgressBar) findViewById(R.id.progressBar1);
//Displaying Progressbar
//progressBar.setVisibility(View.VISIBLE);
//setProgressBarIndeterminateVisibility(true);
//JsonArrayRequest of volley
JsonArrayRequest jsonArrayRequest = new JsonArrayRequest(Config.DATA_URL + String.valueOf(requestCount),
new Response.Listener<JSONArray>() {
@Override
public void onResponse(JSONArray response) {
//Calling method parseData to parse the json response
parseData(response);
//Hiding the progressbar
//progressBar.setVisibility(View.GONE);
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
// progressBar.setVisibility(View.GONE);
//If an error occurs that means end of the list has reached
// Toast.makeText(Hero_main_activity.this, "No More Items Available", Toast.LENGTH_SHORT).show();
}
});
//Returning the request
return jsonArrayRequest;
}
//This method will get data from the web api
private void getData() {
//Adding the method to the queue by calling the method getDataFromServer
requestQueue.add(getDataFromServer(requestCount));
//Incrementing the request counter
requestCount++;
}
//This method will parse json data
private void parseData(JSONArray array) {
for (int i = 0; i < array.length(); i++) {
//Creating the superhero object
SuperHero superHero = new SuperHero();
JSONObject json = null;
try {
//Getting json
json = array.getJSONObject(i);
//Adding data to the superhero object
superHero.setImageUrl(json.getString(Config.TAG_IMAGE_URL));
superHero.setName(json.getString(Config.TAG_NAME));
superHero.setPublisher(json.getString(Config.TAG_PUBLISHER));
} catch (JSONException e) {
e.printStackTrace();
}
//Adding the superhero object to the list
listSuperHeroes.add(superHero);
}
//Notifying the adapter that data has been added or changed
adapter.notifyDataSetChanged();
}
//This method would check that the recyclerview scroll has reached the bottom or not
private boolean isLastItemDisplaying(RecyclerView recyclerView) {
if (recyclerView.getAdapter().getItemCount() != 0) {
int lastVisibleItemPosition = ((LinearLayoutManager) recyclerView.getLayoutManager()).findLastCompletelyVisibleItemPosition();
if (lastVisibleItemPosition != RecyclerView.NO_POSITION && lastVisibleItemPosition == recyclerView.getAdapter().getItemCount() - 1)
return true;
}
return false;
}
//Overriden method to detect scrolling
@Override
public void onScrollChange(View v, int scrollX, int scrollY, int oldScrollX, int oldScrollY) {
//Ifscrolled at last then
if (isLastItemDisplaying(recyclerView)) {
//Calling the method getdata again
getData();
}
}
public interface OnFragmentInteractionListener {
// TODO: Update argument type and name
void onFragmentInteraction(Uri uri);
}
}
答案 0 :(得分:0)
如果我对您的理解正确,那么您想在其他类/活动/片段中调用getData()函数。
这不是最好的方法,但是,您可以创建另一个具有静态变量的类(公共静态Tab1 tab1;),并在Tab1.onCreateView(...)中设置此变量(OtherClass.tab1 = this; )。 (此外,您还必须公开getData函数。)
但是有一个问题,如果一个类/活动/片段试图在Tab1之前到达getData函数,它将给出NullPointEx ...
我不知道您真正想要什么,但是,我认为您必须更改架构。
答案 1 :(得分:0)
这可能不是最好的出路,但您可以肯定......
只需创建TAB1片段类实例即可,该实例应有帮助 如果还没开始之前就实例化该课程
private static Tab1 single_instance = null;
public static Tab1 getInstance(){
if (single_instance == null)
single_instance = new Tab1();
return single_instance;
}
我将假设这是需要调用getData()的其他任何类 通过仅使用TAB1实例变量就可以完成TAB1,就像我们 仅在下面删除。
public class ClassCaller{
Tab1 tab1 = Tab1.getInstance();
这可以是调用程序类中的任何方法,可以是onCreate()或 其他任何人。
@Override
protected void onResume() {
super.onResume();
if(something_is_true)
tab1.getData();
}
}
请记住,我没有测试此代码,只是希望您尝试一下。