2016-12-10 6 views
-1

リストビューで10個のレコードをフェッチし、下からスクロールで他のレコードをフェッチしたい。私はこの新しいことで私を助けてください。フラグメントリストビューの下にscollのjsonデータをフェッチする方法

Latest_Fragment.java

private static final String TAG = LatestFragment.class.getSimpleName(); 

// Movies json url 
private static final String url = "http://abc.news/android/lastest.php?page=1"; 
private ProgressDialog pDialog; 
private List<Movie> movieList = new ArrayList<Movie>(); 

private List<Movie> additems; 
private ListView listView; 
private CustomListAdapter adapter; 
boolean flag_loading = true; 

private static String Title="title"; 
private static String Genre="genre"; 
private static String Rating="rating"; 
private static String Sec_id="secid"; 
private static String Category="category"; 
private static String bitmap="thumbnailUrl"; 
ListView list; 
@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 


    View v = inflater.inflate(R.layout.latest_layout, container, false); 
    //getActivity().getActionBar().setDisplayHomeAsUpEnabled(true); 
    Intent intent2 = new Intent(); 
    getActivity().setResult(Activity.RESULT_OK, intent2); 

    list = (ListView) v.findViewById(R.id.list); 


    adapter = new CustomListAdapter(getActivity(), movieList); 
    list.setAdapter(adapter); 



     pDialog = new ProgressDialog(getActivity()); 
    // Showing progress dialog before making http request 
    pDialog.setMessage("Loading..."); 
    pDialog.show(); 





    // Creating volley request obj 
    JsonArrayRequest movieReq = new JsonArrayRequest(url, 
      new Response.Listener<JSONArray>() { 

       @Override 
       public void onResponse(JSONArray response) { 
        Log.d(TAG, response.toString()); 
        hidePDialog(); 
        // flag_loading = false; 
        // Parsing json 

        for (int i = 0; i < response.length(); i++) { 
         try { 

          JSONObject obj = response.getJSONObject(i); 
          Movie movie = new Movie(); 

          movie.setSec_id(obj.getString("sec_id")); 
          movie.setCategory(obj.getString("category")); 
          movie.setTitle(obj.getString("title")); 
          movie.setThumbnailUrl(obj.getString("image")); 
          movie.setRating(obj.getString("rating")); 
          // movie.setYear(obj.getString("releaseYear")); 
          movie.setGenre(obj.getString("genre")); 

          // Genre is json array 


          // adding movie to movies array 

          movieList.add(movie); 

         } catch (JSONException e) { 
          e.printStackTrace(); 
         } 

        } 

        // notifying list adapter about data changes 
        // so that it renders the list view with updated data 
        adapter.notifyDataSetChanged(); 
       } 
      }, new Response.ErrorListener() { 

     @Override 
     public void onErrorResponse(VolleyError error) { 
      if (error instanceof NoConnectionError){ 
       Toast.makeText(getActivity().getBaseContext(), "Bummer..There's No Internet connection!", Toast.LENGTH_LONG).show(); 

      } 

     } 
    }); 




    // Adding request to request queue 
    AppController.getInstance().addToRequestQueue(movieReq); 

    list.setOnItemClickListener(new AdapterView.OnItemClickListener() { 
     @Override 
     public void onItemClick(AdapterView<?> parent, View view, 
           int position, long id) { 

      Intent intent = new Intent(getActivity(), News_Detail.class); 

      bitmap = ((Movie)movieList.get(position)).getThumbnailUrl(); 
      intent.putExtra("images", bitmap); 

      final String names = ((TextView) view.findViewById(R.id.title)).getText().toString(); 
      intent.putExtra(Title, names); 

      String genredescription = ((TextView) view.findViewById(R.id.genre)).getText().toString(); //must for send data 
      intent.putExtra(Genre, genredescription); 

      String date = ((TextView) view.findViewById(R.id.rating)).getText().toString(); 
      intent.putExtra(Rating, date); 
      startActivity(intent); 



     } 


    }); 



    return v; 


} 





@Override 
public void onDestroy() { 
    super.onDestroy(); 
    hidePDialog(); 
} 

private void hidePDialog() { 
    if (pDialog != null) { 
     pDialog.dismiss(); 
     pDialog = null; 
    } 
} 


public boolean onCreateOptionsMenu(Menu menu) { 
    // Inflate the menu; this adds items to the action bar if it is present. 
    getActivity().getMenuInflater().inflate(R.menu.menu_main, menu); 
    return true; 
} 


} 

customlistadapter.java

private Activity activity; 
private LayoutInflater inflater; 
private List<Movie> movieItems; 
ImageLoader imageLoader = AppController.getInstance().getImageLoader(); 
public CustomListAdapter(Activity activity, List<Movie> movieItems) { 
    this.activity = activity; 
    this.movieItems = movieItems; 
} 

@Override 
public int getCount() { 
    return movieItems.size(); 
} 

@Override 
public Object getItem(int location) { 
    return movieItems.get(location); 
} 

@Override 
public long getItemId(int position) { 
    return position; 
} 

// first row layout change below code is working 
@Override 
public int getViewTypeCount() {  return 2; } 

//if remove below code then chage layout randomly 
@Override 
public int getItemViewType(int position) { 
    if (position == 0) { 
     return 0; 
    } else { 
     return 1; 
    } 
} 

// first row layout change above code is working 

@Override 
public View getView(int position, View convertView, ViewGroup parent) { 





    if (inflater == null) 
     inflater = (LayoutInflater) activity 
       .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 

    if (convertView == null) 

     if(position == 0) { 
      convertView = inflater.inflate(R.layout.list_row2, null); 
     }else 
     { 
      convertView = inflater.inflate(R.layout.list_row, null); 
     } 

    if (imageLoader == null) 
     imageLoader = AppController.getInstance().getImageLoader(); 


    NetworkImageView thumbNail = (NetworkImageView) convertView.findViewById(R.id.thumbnail); 
    final  TextView sec_id = (TextView) convertView.findViewById(R.id.sec_id); 
    final TextView category = (TextView) convertView.findViewById(R.id.category); 
    TextView title = (TextView) convertView.findViewById(R.id.title); 
    TextView rating = (TextView) convertView.findViewById(R.id.rating); 
    TextView genre = (TextView) convertView.findViewById(R.id.genre); 
    //TextView year = (TextView) convertView.findViewById(R.id.releaseYear); 





    // getting movie data for the row 
    Movie m = movieItems.get(position); 

    // thumbnail image 
    thumbNail.setImageUrl(m.getThumbnailUrl(), imageLoader); 

    // title 
    title.setText(m.getTitle()); 

    // rating 
    rating.setText(m.getRating()); 

    // genre 

    genre.setText(m.getGenre()); 

    sec_id.setText(m.getSec_id()); 

    category.setText(m.getCategory()); 





    // release year 
    // year.setText(m.getYear()); 

    ImageView btn = (ImageView) convertView.findViewById(R.id.share); 
    btn.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      Intent intent = new Intent(android.content.Intent.ACTION_SEND); 
      intent.setType("text/plain"); 
      intent.putExtra(Intent.EXTRA_TEXT, "\n" + "\n" + "http://vasundharadeep.news/News/"+sec_id.getText().toString()+"/"+category.getText().toString()); 
      activity.startActivity(intent); 

     } 
    }); 


    return convertView; 

} 




} 

答えて

0

抽象クラス

public abstract class HidingScrollListener implements AbsListView.OnScrollListener { 
    private static final int HIDE_THRESHOLD = 15; 
    public boolean controlsVisible = true; 
    public int scrolledDistance = 0; 
    public int previousTotal = 0; 
    public boolean loading = true; 
    public int current_page = 1; 
    public LinearLayoutManager mLinearLayoutManager; 
    private int visibleThreshold = 2; 

    public HidingScrollListener(LinearLayoutManager layoutManager) { 
     this.mLinearLayoutManager = layoutManager; 
    } 

    @Override public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, 
     int totalItemCount) { 
     // int firstVisibleItem =firstVisibleItem; 

     if (firstVisibleItem == 0) { 
      if (!controlsVisible) { 
       onShow(); 
       controlsVisible = true; 
      } 
     } else { 
      if (scrolledDistance > HIDE_THRESHOLD && controlsVisible) { 
       onHide(); 
       controlsVisible = false; 
       scrolledDistance = 0; 
      } else if (scrolledDistance < -HIDE_THRESHOLD && !controlsVisible) { 
       onShow(); 
       controlsVisible = true; 
       scrolledDistance = 0; 
      } 
     } 


     if (loading) { 
      if (totalItemCount > previousTotal) { 
       loading = false; 
       previousTotal = totalItemCount; 
      } 
     } 

     if (!loading && (totalItemCount - visibleItemCount) <= (firstVisibleItem + visibleThreshold)) { 
      current_page++; 

      onLoadMore(current_page); 

      loading = true; 
     } 
    } 



    public abstract void onHide(); 

    public abstract void onShow(); 

    public abstract void onLoadMore(int current_page); 
} 

// Current Page getter setter 


public int getCurrentPage() { 
     return currentPage; 
    } 

public void setCurrentPage(int currentPage) { 
    this.currentPage = currentPage; 
} 
+0

コードはあなたに私はplsはトリングてるOK離れて行くいけない –

+0

:)アンドロイドで新たなんだ、私のリストビューでこれを使うリストでRecyclerViewを交換する方法を教えてください:) –

+0

を支援する場合は受け入れるとupvote、私は –

0

O、それを確認してください:ここでは、Javaコードとアダプタの私のフラグメントですコードの下UTは

public class MyFragment extends Fragment implements  OnScrollListener { 
private ListView listView; 
private ArrayList<DataModel> dataList = new ArrayList<DataModel>(); 
private boolean mHasRequestedMore; 

onCreateView(…..){ 
listView = view.findViewById(); 
listView.setonScrollLestener(this); 
callWebservice(); 
} 





@Override 
public void onScroll(final AbsListView view, final int firstVisibleItem, final int visibleItemCount, final int totalItemCount) { 
if (!mHasRequestedMore) { 
    int lastInScreen = firstVisibleItem + visibleItemCount; 
    if (lastInScreen >= totalItemCount) { 
     mHasRequestedMore = true; 
    callWebservice(); 
    } 
} 
} 
@Override 
public void onScrollStateChanged(AbsListView view, int scrollState{}  
関連する問題