跳過佈局我使用Recycle
擺脫JSON URL數據包括TextView
和ImageView
我有一些類:Activity
,POJO
和Adapter
。RecyclerView:沒有附加適配器;在回收站的Android
這是我的源代碼:(修訂)
public class PartOneActivity extends AppCompatActivity {
private EditText editTextSearcch;
List<GetDataAdapter> GetDataAdapter1;
RecyclerView recyclerView;
RecyclerView.LayoutManager recyclerViewlayoutManager;
RecyclerView.Adapter recyclerViewadapter;
String GET_JSON_DATA_HTTP_URL = "myURL";
String JSON_IMAGE_TITLE_NAME = "name";
String JSON_IMAGE_URL = "thumb";
JsonArrayRequest jsonArrayRequest ;
RequestQueue requestQueue ;
@Override
protected void onCreate(Bundle savedInstanceState) {
setTheme(R.style.AppThemeRed);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
initEditTextSearch();
initRecyclerView();
}
private void initEditTextSearch() {
/*mToolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(mToolbar);
setTitle(getString(R.string.app_name));
mToolbar.setTitleTextColor(getResources().getColor(android.R.color.white));*/
editTextSearcch = (EditText) findViewById(R.id.editTextSearch);
editTextSearcch.setVisibility(View.VISIBLE);
}
private void initRecyclerView() {
/*
RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recyclerView);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
RecyclerAdapter recyclerAdapter = new RecyclerAdapter(createItemList());
recyclerView.setAdapter(recyclerAdapter);
*/
GetDataAdapter1 = new ArrayList<>();
recyclerView = (RecyclerView) findViewById(R.id.recyclerView);
recyclerViewadapter = new RecyclerViewAdapter(GetDataAdapter1, this);
recyclerView.setAdapter(recyclerViewadapter);
// recyclerView.setHasFixedSize(true);
recyclerViewlayoutManager = new LinearLayoutManager(this);
recyclerView.setLayoutManager(recyclerViewlayoutManager);
// JSON_DATA_WEB_CALL();
json_data_web_call();
recyclerView.addOnScrollListener(new HidingScrollListener() {
@Override
public void onHide() {
hideViews();
}
@Override
public void onShow() {
showViews();
}
});
}
private void hideViews() {
editTextSearcch.animate().translationY(-editTextSearcch.getHeight()).setInterpolator(new AccelerateInterpolator(2));
}
private void showViews() {
editTextSearcch.animate().translationY(0).setInterpolator(new DecelerateInterpolator(2));
}
public void json_data_web_call(){
jsonArrayRequest = new JsonArrayRequest(GET_JSON_DATA_HTTP_URL,
new Response.Listener<JSONArray>() {
@Override
public void onResponse(JSONArray response) {
json_parse_data_after_webcall(response);
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
});
requestQueue = Volley.newRequestQueue(this);
requestQueue.add(jsonArrayRequest);
}
public void json_parse_data_after_webcall(JSONArray array){
for(int i = 0; i<array.length(); i++) {
GetDataAdapter GetDataAdapter2 = new GetDataAdapter();
JSONObject json = null;
try {
json = array.getJSONObject(i);
GetDataAdapter2.setImageTitleNamee(json.getString(JSON_IMAGE_TITLE_NAME));
GetDataAdapter2.setImageServerUrl(json.getString(JSON_IMAGE_URL));
} catch (JSONException e) {
e.printStackTrace();
}
GetDataAdapter1.add(GetDataAdapter2);
}
recyclerViewadapter.set(GetDataAdapter1); //may need to add a setter in adapter
recyclerViewadapter.notifyDataSetChanged();
// recyclerViewadapter = new RecyclerViewAdapter(GetDataAdapter1, this);
// recyclerView.setAdapter(recyclerViewadapter);
}
}
RecyclerViewAdapter類:
public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.ViewHolder> {
private static final int TYPE_HEADER = 4;
private static final int TYPE_ITEM = 1;
Context context;
List<GetDataAdapter> getDataAdapter;
ImageLoader imageLoader1;
public RecyclerViewAdapter(List<GetDataAdapter> getDataAdapter, Context context){
super();
this.getDataAdapter = getDataAdapter;
this.context = context;
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.recyclerview_items, parent, false);
ViewHolder viewHolder = new ViewHolder(v);
return viewHolder;
}
@Override
public void onBindViewHolder(ViewHolder Viewholder, int position) {
if (!isPositionHeader(position)) {
GetDataAdapter getDataAdapter1 = getDataAdapter.get(position - 1);
imageLoader1 = ServerImageParseAdapter.getInstance(context).getImageLoader();
imageLoader1.get(getDataAdapter1.getImageServerUrl(),
ImageLoader.getImageListener(
Viewholder.networkImageView,//Server Image
R.mipmap.ic_launcher,//Before loading server image the default showing image.
android.R.drawable.ic_dialog_alert //Error image if requested image dose not found on server.
)
); Viewholder.networkImageView.setImageUrl(getDataAdapter1.getImageServerUrl(), imageLoader1);
Viewholder.ImageTitleNameView.setText(getDataAdapter1.getImageTitleName());
}
}
public int getDataAdapter() {
return getDataAdapter == null ? 0 : getDataAdapter.size();
}
@Override
public int getItemViewType(int position) {
if (isPositionHeader(position)) {
return TYPE_HEADER;
}
return TYPE_ITEM;
}
@Override
public int getItemCount() {
return getDataAdapter() + 1;
}
private boolean isPositionHeader(int position) {
return position == 0;
}
class ViewHolder extends RecyclerView.ViewHolder{
public TextView ImageTitleNameView;
public NetworkImageView networkImageView ;
public ViewHolder(View itemView) {
super(itemView);
ImageTitleNameView = (TextView) itemView.findViewById(R.id.textView_item) ;
networkImageView = (NetworkImageView) itemView.findViewById(R.id.VollyNetworkImageView1) ;
}
}
}
當我建立我看到Recycler
無法加載TextView
和ImageView
,和CatLog
出現以下異常:
E/RecyclerView: No adapter attached; skipping layout
E/RecyclerView: No adapter attached; skipping layout
如何解決這個異常?
方法的名稱不應該大寫鎖定,web請求開始 –
前recyclerview認爲你沒有本質上的適配器,因爲你只設置你得到的數據後,你可以設置適配器。初始化它,然後創建一個setter並將數據設置爲適配器。 –