| | |
| | | package com.weikou.beibeivideo.ui.recent; |
| | | |
| | | import android.database.Cursor; |
| | | import android.os.Bundle; |
| | | import android.support.v4.app.Fragment; |
| | | import android.support.v4.app.LoaderManager; |
| | | import android.support.v4.content.CursorLoader; |
| | | import android.support.v4.content.Loader; |
| | | import android.support.v4.widget.SwipeRefreshLayout; |
| | | import android.view.LayoutInflater; |
| | | import android.view.View; |
| | | import android.view.ViewGroup; |
| | | import android.widget.ListView; |
| | | import android.widget.ProgressBar; |
| | | import android.widget.Toast; |
| | | |
| | | import com.google.gson.FieldNamingPolicy; |
| | | import com.google.gson.Gson; |
| | | import com.google.gson.GsonBuilder; |
| | | import com.google.gson.reflect.TypeToken; |
| | | import com.lcjian.library.util.RefreshLayout; |
| | | import com.umeng.analytics.MobclickAgent; |
| | | import com.weikou.beibeivideo.BasicTextHttpResponseHandler; |
| | | import com.weikou.beibeivideo.BeibeiVideoAPI; |
| | | import com.weikou.beibeivideo.BeibeiVideoApplication; |
| | | import com.weikou.beibeivideo.R; |
| | | import com.weikou.beibeivideo.db.WatchHistoryTable; |
| | | import com.weikou.beibeivideo.entity.vo.WatchHistoryVO; |
| | | import com.weikou.beibeivideo.util.UserUtil; |
| | | |
| | | import org.apache.http.Header; |
| | | import org.json.JSONObject; |
| | | |
| | | import java.lang.reflect.Type; |
| | | import java.util.ArrayList; |
| | | import java.util.List; |
| | | import java.util.Set; |
| | | |
| | | public class WatchHistoryFragment extends Fragment implements |
| | | LoaderManager.LoaderCallbacks<Cursor> { |
| | | public class WatchHistoryFragment extends Fragment { |
| | | |
| | | private RefreshLayout srl; |
| | | |
| | | private WatchHistoryAdapter mAdapter; |
| | | |
| | |
| | | |
| | | private View empty; |
| | | |
| | | private List<WatchHistoryVO> voList = new ArrayList<>(); |
| | | |
| | | private int currentPage = 1; |
| | | |
| | | private long count = 0; |
| | | |
| | | public void setDeleteCallback( |
| | | DownloadAdapter2.IGetDeleteCallback getdeleteNumber) { |
| | | this.getdeleteNumber = getdeleteNumber; |
| | |
| | | mAdapter.setDeleteCallback(getdeleteNumber); |
| | | } |
| | | |
| | | private void initView(View view){ |
| | | list = view.findViewById(R.id.list); |
| | | list.setDividerHeight(1); |
| | | mAdapter = new WatchHistoryAdapter(list.getContext(), voList); |
| | | if (getdeleteNumber != null) |
| | | mAdapter.setDeleteCallback(getdeleteNumber); |
| | | list.setAdapter(mAdapter); |
| | | |
| | | srl = view.findViewById(R.id.srl_watch_history); |
| | | ProgressBar pb = new ProgressBar(getContext()); |
| | | srl.setFooter(pb); |
| | | srl.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() { |
| | | @Override |
| | | public void onRefresh() { |
| | | currentPage = 1; |
| | | getWatchHistory(currentPage); |
| | | } |
| | | }); |
| | | srl.setOnLoadListener(new RefreshLayout.OnLoadListener() { |
| | | @Override |
| | | public void onLoad() { |
| | | getWatchHistory(currentPage+1); |
| | | } |
| | | }); |
| | | srl.setRefreshing(true); |
| | | |
| | | |
| | | |
| | | getWatchHistory(currentPage); |
| | | } |
| | | |
| | | @Override |
| | | public View onCreateView(LayoutInflater inflater, ViewGroup container, |
| | | Bundle savedInstanceState) { |
| | | return inflater.inflate(R.layout.watch_history_fragment, container, |
| | | View view = inflater.inflate(R.layout.watch_history_fragment, container, |
| | | false); |
| | | return view; |
| | | } |
| | | |
| | | @Override |
| | | public void onViewCreated(View view, Bundle savedInstanceState) { |
| | | super.onViewCreated(view, savedInstanceState); |
| | | |
| | | empty = view.findViewById(R.id.fl_empty); |
| | | // empty.setOnClickListener(new OnClickListener() { |
| | | // |
| | | // @Override |
| | | // public void onClick(View v) { |
| | | // Intent intent = new Intent(v.getContext(), |
| | | // MVideosActivity.class); |
| | | // intent.putExtra("home_type", "4"); |
| | | // intent.putExtra("title", "内部福利"); |
| | | // v.getContext().startActivity(intent); |
| | | // } |
| | | // }); |
| | | |
| | | list = (ListView) view.findViewById(R.id.list); |
| | | list.setDividerHeight(1); |
| | | // list.setOnItemClickListener(this); |
| | | mAdapter = new WatchHistoryAdapter(list.getContext(), getActivity().getApplicationContext(), null, true); |
| | | if (getdeleteNumber != null) |
| | | mAdapter.setDeleteCallback(getdeleteNumber); |
| | | list.setAdapter(mAdapter); |
| | | // Prepare the loader. Either re-connect with an existing one, |
| | | // or start a new one. |
| | | getLoaderManager().initLoader(0, null, this); |
| | | } |
| | | |
| | | @Override |
| | | public Loader<Cursor> onCreateLoader(int arg0, Bundle arg1) { |
| | | return new CursorLoader(list.getContext(), WatchHistoryTable.CONTENT_URI, |
| | | null, null, null, WatchHistoryTable.UPDATE_TIME + " DESC"); |
| | | |
| | | |
| | | } |
| | | |
| | | @Override |
| | |
| | | MobclickAgent.onPageEnd("主页面——最近——观看记录"); |
| | | } |
| | | |
| | | @Override |
| | | public void onLoadFinished(Loader<Cursor> arg0, Cursor data) { |
| | | // Swap the new cursor in. (The framework will take care of closing the |
| | | // old cursor once we return.) |
| | | mAdapter.swapCursor(data); |
| | | if (mAdapter.getCount() == 0) { |
| | | empty.setVisibility(View.VISIBLE); |
| | | } else { |
| | | empty.setVisibility(View.INVISIBLE); |
| | | } |
| | | } |
| | | |
| | | @Override |
| | | public void onLoaderReset(Loader<Cursor> arg0) { |
| | | // This is called when the last Cursor provided to onLoadFinished() |
| | | // above is about to be closed. We need to make sure we are no |
| | | // longer using it. |
| | | mAdapter.swapCursor(null); |
| | | } |
| | | |
| | | |
| | | public void deleteWatchHistory() { |
| | | List<Long> selectedIds = mAdapter.getSelectedIds(); |
| | | Set<String> selectedIds = mAdapter.getSelectedIds(); |
| | | if (!selectedIds.isEmpty()) { |
| | | for (int i = 0; i < selectedIds.size(); i++) { |
| | | list.getContext().getContentResolver().delete( |
| | | WatchHistoryTable.CONTENT_URI, |
| | | WatchHistoryTable._ID + " = ? ", |
| | | new String[]{String.valueOf(selectedIds.get(i))}); |
| | | } |
| | | mAdapter.getSelectedIds().clear(); |
| | | deleteWatchHistory(selectedIds); |
| | | } else { |
| | | Toast.makeText(list.getContext(), "请选择要删除的记录", Toast.LENGTH_LONG) |
| | | .show(); |
| | |
| | | public int getCount() { |
| | | return mAdapter.getCount(); |
| | | } |
| | | |
| | | |
| | | private void getWatchHistory(int page) { |
| | | BeibeiVideoAPI.getWatchHistory(BeibeiVideoApplication.application, UserUtil.getUid(BeibeiVideoApplication.application), page, new BasicTextHttpResponseHandler() { |
| | | @Override |
| | | public void onSuccessPerfect(int statusCode, Header[] headers, JSONObject jsonObject) throws Exception { |
| | | if (jsonObject.optBoolean("IsPost")) { |
| | | Gson gson = new GsonBuilder().setFieldNamingPolicy( |
| | | FieldNamingPolicy.UPPER_CAMEL_CASE) |
| | | .create(); |
| | | JSONObject data = jsonObject.optJSONObject("Data"); |
| | | count = data.optLong("count"); |
| | | Type type = new TypeToken<List<WatchHistoryVO>>() { |
| | | }.getType(); |
| | | |
| | | List<WatchHistoryVO> list = gson.fromJson(data.optJSONArray("list").toString(), type); |
| | | currentPage = page; |
| | | if (page == 1) |
| | | voList.clear(); |
| | | if (list != null && list.size() > 0) { |
| | | voList.addAll(list); |
| | | } |
| | | mAdapter.notifyDataSetChanged(); |
| | | } |
| | | } |
| | | }); |
| | | } |
| | | |
| | | private void deleteWatchHistory(Set<String> ids) { |
| | | |
| | | BeibeiVideoAPI.deleteWatchHistory(BeibeiVideoApplication.application, UserUtil.getUid(BeibeiVideoApplication.application), ids, new BasicTextHttpResponseHandler() { |
| | | @Override |
| | | public void onSuccessPerfect(int statusCode, Header[] headers, JSONObject jsonObject) throws Exception { |
| | | if (jsonObject.optBoolean("IsPost")) { |
| | | for (int i = 0; i < voList.size(); i++) { |
| | | if (ids.contains(voList.get(i).getId())) { |
| | | voList.remove(i--); |
| | | } |
| | | } |
| | | mAdapter.getSelectedIds().clear(); |
| | | mAdapter.notifyDataSetChanged(); |
| | | } else { |
| | | Toast.makeText(BeibeiVideoApplication.application, jsonObject.optString("Error"), Toast.LENGTH_SHORT).show(); |
| | | } |
| | | |
| | | } |
| | | }); |
| | | |
| | | } |
| | | |
| | | } |