如何在列表视图中使用滑动列表适配器和滚动列表适配器

时间:2016-08-01 03:01:28

标签: android listview

我正在开发一个找到了滑动列表功能Here的Android应用程序,现在我已经在列表视图中添加了一个快速滚动功能here

这是我的代码:

public class HomeList扩展了Fragment工具         SwipeRefreshLayout.OnRefreshListener {

private String TAG = HomeList.class.getSimpleName();

private String URL_TOP_250 = "http://api.androidhive.info/json/imdb_top_250.php?offset=";

private SwipeRefreshLayout swipeRefreshLayout;
private ListView listView;
private SwipeListAdapter adapter;
private List<Movie> movieList;

// initially offset will be 0, later will be updated while parsing the json
private int offSet = 0;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    View rootView = inflater.inflate(R.layout.activity_home_list,
            container, false);

    listView = (ListView) rootView.findViewById(R.id.listView);

    listView.setFastScrollEnabled(true);

    swipeRefreshLayout = (SwipeRefreshLayout) rootView
            .findViewById(R.id.swipe_refresh_layout);

    movieList = new ArrayList<>();
    adapter = new SwipeListAdapter(getActivity(), movieList);
    listView.setAdapter(adapter);

    swipeRefreshLayout.setOnRefreshListener(this);

    /**
     * Showing Swipe Refresh animation on activity create As animation won't
     * start on onCreate, post runnable is used
     */
    swipeRefreshLayout.post(new Runnable() {
        @Override
        public void run() {
            swipeRefreshLayout.setRefreshing(true);

            fetchMovies();
        }
    });

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

            Intent i = new Intent(getActivity(), AddMain.class);
            startActivity(i);

        }
    });

    FrameLayout footerLayout = (FrameLayout) getActivity()
            .getLayoutInflater().inflate(R.layout.footerview, null);
    Button btnPostYourEnquiry = (Button) footerLayout
            .findViewById(R.id.btnGetMoreResults);

    btnPostYourEnquiry.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent i = new Intent(getActivity(), AddMain.class);
            startActivity(i);
        }
    });

    listView.addFooterView(footerLayout);

    return rootView;

}

/**
 * This method is called when swipe refresh is pulled down
 */
@Override
public void onRefresh() {
    fetchMovies();
}

/**
 * Fetching movies json by making http call
 */
private void fetchMovies() {

    // showing refresh animation before making http call
    swipeRefreshLayout.setRefreshing(true);

    // appending offset to url
    String url = URL_TOP_250 + offSet;

    // Volley's json array request object
    JsonArrayRequest req = new JsonArrayRequest(url,
            new Response.Listener<JSONArray>() {
                @Override
                public void onResponse(JSONArray response) {
                    Log.d(TAG, response.toString());

                    if (response.length() > 0) {

                        // looping through json and adding to movies list
                        for (int i = 0; i < response.length(); i++) {
                            try {
                                JSONObject movieObj = response
                                        .getJSONObject(i);

                                int rank = movieObj.getInt("rank");
                                String title = movieObj.getString("title");
                                Movie m = new Movie(rank, title);

                                movieList.add(0, m);

                                // updating offset value to highest value
                                if (rank >= offSet)
                                    offSet = rank;

                            } catch (JSONException e) {
                                Log.e(TAG,
                                        "JSON Parsing error: "
                                                + e.getMessage());
                            }
                        }

                        adapter.notifyDataSetChanged();
                    }

                    // stopping swipe refresh
                    swipeRefreshLayout.setRefreshing(false);

                }
            }, new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    Log.e(TAG, "Server Error: " + error.getMessage());

                    Toast.makeText(getActivity(), error.getMessage(),
                            Toast.LENGTH_LONG).show();

                    // stopping swipe refresh
                    swipeRefreshLayout.setRefreshing(false);
                }
            });

    // Adding request to request queue
    MyApplication.getInstance().addToRequestQueue(req);
}

class MyListAdaptor extends ArrayAdapter<String> implements SectionIndexer {

    HashMap<String, Integer> alphaIndexer;
    String[] sections;

    public MyListAdaptor(Context context, LinkedList<String> items) {  
super(context, R.layout.scrol, items);  

alphaIndexer = new HashMap<String, Integer>();  
int size = items.size();  

for (int x = 0; x < size; x++) {  
     String s = items.get(x);  

     // get the first letter of the store  
     String ch = s.substring(0, 1);  
     // convert to uppercase otherwise lowercase a -z will be sorted  
     // after upper A-Z  
     ch = ch.toUpperCase();  
     if (!alphaIndexer.containsKey(ch))
     alphaIndexer.put(ch, x);
    }  

            Set<String> sectionLetters = alphaIndexer.keySet();  

            // create a list from the set to sort  
            ArrayList<String> sectionList = new ArrayList<String>(  
                      sectionLetters);  

            Collections.sort(sectionList);  

            sections = new String[sectionList.size()];  

            sectionList.toArray(sections);  
       }        

    public int getPositionForSection(int section) {
        return alphaIndexer.get(sections[section]);
    }

    public int getSectionForPosition(int position) {
        return 0;
    }

    public Object[] getSections() {
        return sections;
    }
}

}

我不知道如何将两个适配器结合起来,有人可以帮助我吗?

0 个答案:

没有答案