File was renamed from library-BaiduCPU/src/main/java/com/yeshi/ec/library_baiducpu/fragment/BaiDuNewsContentFragment.java |
| | |
| | | package com.yeshi.ec.library_baiducpu.fragment; |
| | | |
| | | import android.content.Context; |
| | | import android.graphics.Canvas; |
| | | import android.graphics.Color; |
| | | import android.graphics.Rect; |
| | | import android.os.Bundle; |
| | | import android.support.annotation.NonNull; |
| | | import android.support.v4.widget.SwipeRefreshLayout; |
| | | import android.support.v7.widget.GridLayoutManager; |
| | | import android.support.v7.widget.LinearLayoutManager; |
| | | import android.support.v7.widget.RecyclerView; |
| | | import android.util.Log; |
| | | import android.view.Gravity; |
| | | import android.view.LayoutInflater; |
| | | import android.view.View; |
| | | import android.view.ViewGroup; |
| | | import android.widget.RelativeLayout; |
| | | import android.widget.TextView; |
| | | |
| | | import com.androidquery.AQuery; |
| | | import com.baidu.mobad.feeds.RequestParameters; |
| | | import com.baidu.mobads.nativecpu.CPUAdRequest; |
| | | import com.baidu.mobads.nativecpu.CpuLpFontSize; |
| | |
| | | import com.lcjian.library.util.common.DimenUtils; |
| | | import com.ut.device.UTDevice; |
| | | import com.yeshi.ec.library_baiducpu.R; |
| | | import com.yeshi.ec.library_baiducpu.entity.NativeCPUView; |
| | | import com.yeshi.ec.library_baiducpu.adapter.BaiduCPUContentAdapter; |
| | | import com.yeshi.ec.library_baiducpu.entity.SpinnerItem; |
| | | |
| | | import java.util.ArrayList; |
| | |
| | | /** |
| | | * 百度资讯 |
| | | */ |
| | | public class BaiDuNewsContentFragment extends RetainViewFragment implements NativeCPUManager.CPUAdListener { |
| | | public class BaiDuCPUContentFragment extends RetainViewFragment implements NativeCPUManager.CPUAdListener { |
| | | |
| | | public final static int VIEW_TYPE_NEWS = 1; |
| | | public final static int VIEW_TYPE_SMALLVIDEO = 2; |
| | | public final static int VIEW_TYPE_VIDEO = 3; |
| | | |
| | | private final String YOUR_APP_ID = "be796ac7"; // 双引号中填写自己的APPSID |
| | | private List<IBasicCPUData> nrAdList = new ArrayList<IBasicCPUData>(); |
| | | private NativeCPUManager mCpuManager; |
| | |
| | | |
| | | private boolean isLoad; |
| | | |
| | | private MyAdapter adapter; |
| | | private BaiduCPUContentAdapter adapter; |
| | | |
| | | private final static String TAG = "BaiDuNewsContent"; |
| | | |
| | | private TextView footerView; |
| | | |
| | | private int viewType; |
| | | |
| | | public static BaiDuNewsContentFragment newInstance(SpinnerItem spinnerItem) { |
| | | BaiDuNewsContentFragment fragment = new BaiDuNewsContentFragment(); |
| | | |
| | | public static BaiDuCPUContentFragment newInstance(SpinnerItem spinnerItem, int viewType) { |
| | | BaiDuCPUContentFragment fragment = new BaiDuCPUContentFragment(); |
| | | Bundle bundle = new Bundle(); |
| | | bundle.putSerializable("item", spinnerItem); |
| | | bundle.putInt("viewType", viewType); |
| | | fragment.setArguments(bundle); |
| | | return fragment; |
| | | } |
| | |
| | | isLoad = true; |
| | | if (nrAdList != null && nrAdList.size() > 0) |
| | | footerView.setVisibility(View.VISIBLE); |
| | | else |
| | | footerView.setVisibility(View.GONE); |
| | | loadAd(currentPage); |
| | | } |
| | | } |
| | |
| | | footerView.setTextSize(12); |
| | | } |
| | | |
| | | RecyclerView.ItemDecoration itemDecoration2 = new RecyclerView.ItemDecoration() { |
| | | @Override |
| | | public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) { |
| | | super.onDraw(c, parent, state); |
| | | } |
| | | |
| | | @Override |
| | | public void onDrawOver(Canvas c, RecyclerView parent, RecyclerView.State state) { |
| | | super.onDrawOver(c, parent, state); |
| | | } |
| | | |
| | | @Override |
| | | public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) { |
| | | super.getItemOffsets(outRect, view, parent, state); |
| | | int index = parent.getChildAdapterPosition(view); |
| | | int total = parent.getAdapter().getItemCount(); |
| | | int minWidth = DimenUtils.dipToPixels(2, view.getContext()); |
| | | if (index % 2 == 0) { |
| | | outRect.left = 0; //第一列左边贴边 |
| | | outRect.right = minWidth; |
| | | } else { |
| | | outRect.right = 0; //第一列左边贴边 |
| | | outRect.left = minWidth; |
| | | } |
| | | |
| | | if (index - 1 > 0) { |
| | | outRect.top = minWidth; |
| | | } else |
| | | outRect.top = 0; |
| | | |
| | | if (total % 2 == 0) { |
| | | if (total > 2 && (index < total - 2)) |
| | | outRect.bottom = minWidth; |
| | | else |
| | | outRect.bottom = 0; |
| | | } else { |
| | | if (total > 2 && (index < total - 1)) |
| | | outRect.bottom = minWidth; |
| | | else |
| | | outRect.bottom = 0; |
| | | } |
| | | } |
| | | }; |
| | | |
| | | |
| | | private void initData() { |
| | | Bundle bundle = getArguments(); |
| | | SpinnerItem item = (SpinnerItem) bundle.getSerializable("item"); |
| | | viewType = bundle.getInt("viewType"); |
| | | bundle.clear(); |
| | | mChannelId = item.getmId(); |
| | | |
| | |
| | | */ |
| | | mCpuManager.setLpFontSize(CpuLpFontSize.SMALL); |
| | | mCpuManager.setLpDarkMode(true); |
| | | adapter = new MyAdapter(getContext()); |
| | | adapter = new BaiduCPUContentAdapter(getContext(), viewType, nrAdList); |
| | | rv_news.setAdapter(adapter); |
| | | rv_news.setLayoutManager(new LinearLayoutManager(getContext())); |
| | | |
| | | if (viewType == VIEW_TYPE_SMALLVIDEO) { |
| | | RecyclerView.LayoutManager layoutManager = new GridLayoutManager(getContext(), 2); |
| | | rv_news.setLayoutManager(layoutManager); |
| | | //统一分为2列 |
| | | ((GridLayoutManager) rv_news.getLayoutManager()).setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() { |
| | | @Override |
| | | public int getSpanSize(int position) { |
| | | if (position >= nrAdList.size()) |
| | | return 2; |
| | | else |
| | | return 1; |
| | | } |
| | | }); |
| | | rv_news.addItemDecoration(itemDecoration2); |
| | | } else { |
| | | rv_news.setLayoutManager(new LinearLayoutManager(getContext())); |
| | | } |
| | | |
| | | srl_news.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() { |
| | | @Override |
| | | public void onRefresh() { |
| | |
| | | @Override |
| | | public void onAdLoaded(List<IBasicCPUData> list) { |
| | | onLoadFinish(); |
| | | for (int i = list.size() - 1; i >= 0; i--) { |
| | | if ("ad".equalsIgnoreCase(list.get(i).getType())) { |
| | | list.remove(i--); |
| | | break; |
| | | } |
| | | } |
| | | |
| | | int start = nrAdList.size(); |
| | | if (currentPage == 1) { |
| | | nrAdList.clear(); |
| | |
| | | @Override |
| | | public void onAdStatusChanged(String s) { |
| | | |
| | | } |
| | | |
| | | class MyAdapter extends RecyclerView.Adapter { |
| | | private LayoutInflater inflater; |
| | | private AQuery aq; |
| | | private View footerView; |
| | | |
| | | private final int TYPE_FOOTER = 1; |
| | | private final int TYPE_CONTENT = 2; |
| | | |
| | | |
| | | public MyAdapter(Context context) { |
| | | inflater = LayoutInflater.from(getContext()); |
| | | aq = new AQuery(context); |
| | | } |
| | | |
| | | public void setFooterView(View footerView) { |
| | | this.footerView = footerView; |
| | | } |
| | | |
| | | |
| | | @NonNull |
| | | @Override |
| | | public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) { |
| | | if (viewType == TYPE_CONTENT) |
| | | return new ViewHolder(inflater.inflate(R.layout.item_baidu_news, null, false)); |
| | | else |
| | | return new FooterViewHolder(footerView); |
| | | } |
| | | |
| | | @Override |
| | | public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) { |
| | | if (holder instanceof ViewHolder) { |
| | | ViewHolder viewHolder = (ViewHolder) holder; |
| | | final IBasicCPUData nrAd = nrAdList.get(position); |
| | | final NativeCPUView cpuView = new NativeCPUView(getContext()); |
| | | if (cpuView.getParent() != null) { |
| | | ((ViewGroup) cpuView.getParent()).removeView(cpuView); |
| | | } |
| | | cpuView.setItemData(nrAd, aq); |
| | | viewHolder.rl_container.removeAllViews(); |
| | | viewHolder.rl_container.addView(cpuView); |
| | | nrAd.onImpression(viewHolder.view); |
| | | viewHolder.view.setOnClickListener(new View.OnClickListener() { |
| | | @Override |
| | | public void onClick(View view) { |
| | | nrAd.handleClick(view); |
| | | } |
| | | }); |
| | | } |
| | | } |
| | | |
| | | @Override |
| | | public int getItemCount() { |
| | | int count = nrAdList.size(); |
| | | if (footerView != null) |
| | | count++; |
| | | return count; |
| | | } |
| | | |
| | | @Override |
| | | public int getItemViewType(int position) { |
| | | if (position >= nrAdList.size()) |
| | | return TYPE_FOOTER; |
| | | else |
| | | return TYPE_CONTENT; |
| | | } |
| | | |
| | | class ViewHolder extends RecyclerView.ViewHolder { |
| | | RelativeLayout rl_container; |
| | | View view; |
| | | |
| | | public ViewHolder(View itemView) { |
| | | super(itemView); |
| | | view = itemView; |
| | | rl_container = itemView.findViewById(R.id.native_outer_view); |
| | | } |
| | | } |
| | | |
| | | class FooterViewHolder extends RecyclerView.ViewHolder { |
| | | public FooterViewHolder(View itemView) { |
| | | super(itemView); |
| | | } |
| | | } |
| | | } |
| | | |
| | | } |