mirror of https://github.com/FongMi/TV.git
parent
a4fefc2b85
commit
59f792c752
@ -0,0 +1,19 @@ |
||||
package com.fongmi.bear.bean; |
||||
|
||||
import com.google.gson.annotations.SerializedName; |
||||
|
||||
public class Type { |
||||
|
||||
@SerializedName("type_id") |
||||
private String typeId; |
||||
@SerializedName("type_name") |
||||
private String typeName; |
||||
|
||||
public String getTypeId() { |
||||
return typeId; |
||||
} |
||||
|
||||
public String getTypeName() { |
||||
return typeName; |
||||
} |
||||
} |
||||
@ -0,0 +1,81 @@ |
||||
package com.fongmi.bear.ui.activity; |
||||
|
||||
import android.app.Activity; |
||||
import android.content.Intent; |
||||
|
||||
import androidx.annotation.NonNull; |
||||
import androidx.fragment.app.Fragment; |
||||
import androidx.fragment.app.FragmentActivity; |
||||
import androidx.viewbinding.ViewBinding; |
||||
import androidx.viewpager2.adapter.FragmentStateAdapter; |
||||
|
||||
import com.fongmi.bear.bean.Result; |
||||
import com.fongmi.bear.bean.Type; |
||||
import com.fongmi.bear.databinding.ActivityVodBinding; |
||||
import com.fongmi.bear.ui.fragment.VodFragment; |
||||
import com.google.android.material.tabs.TabLayout; |
||||
|
||||
public class VodActivity extends BaseActivity { |
||||
|
||||
private ActivityVodBinding mBinding; |
||||
private Result mResult; |
||||
|
||||
private String getResult() { |
||||
return getIntent().getStringExtra("result"); |
||||
} |
||||
|
||||
public static void start(Activity activity, Result result) { |
||||
Intent intent = new Intent(activity, VodActivity.class); |
||||
intent.putExtra("result", result.toString()); |
||||
activity.startActivity(intent); |
||||
} |
||||
|
||||
@Override |
||||
protected ViewBinding getBinding() { |
||||
return mBinding = ActivityVodBinding.inflate(getLayoutInflater()); |
||||
} |
||||
|
||||
@Override |
||||
protected void initView() { |
||||
mResult = Result.objectFrom(getResult()); |
||||
for (Type type : mResult.getTypes()) mBinding.tab.addTab(mBinding.tab.newTab().setText(type.getTypeName())); |
||||
mBinding.pager.setAdapter(new PageAdapter(this)); |
||||
mBinding.pager.setOffscreenPageLimit(mResult.getTypes().size()); |
||||
} |
||||
|
||||
@Override |
||||
protected void initEvent() { |
||||
mBinding.tab.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() { |
||||
@Override |
||||
public void onTabSelected(TabLayout.Tab tab) { |
||||
mBinding.pager.setCurrentItem(tab.getPosition()); |
||||
} |
||||
|
||||
@Override |
||||
public void onTabUnselected(TabLayout.Tab tab) { |
||||
} |
||||
|
||||
@Override |
||||
public void onTabReselected(TabLayout.Tab tab) { |
||||
} |
||||
}); |
||||
} |
||||
|
||||
public class PageAdapter extends FragmentStateAdapter { |
||||
|
||||
public PageAdapter(@NonNull FragmentActivity activity) { |
||||
super(activity); |
||||
} |
||||
|
||||
@NonNull |
||||
@Override |
||||
public Fragment createFragment(int position) { |
||||
return VodFragment.newInstance(mResult.getTypes().get(position).getTypeId()); |
||||
} |
||||
|
||||
@Override |
||||
public int getItemCount() { |
||||
return mResult.getTypes().size(); |
||||
} |
||||
} |
||||
} |
||||
@ -0,0 +1,158 @@ |
||||
package com.fongmi.bear.ui.custom; |
||||
|
||||
import android.content.Context; |
||||
import android.util.AttributeSet; |
||||
import android.view.LayoutInflater; |
||||
import android.view.View; |
||||
import android.view.ViewGroup; |
||||
import android.widget.RelativeLayout; |
||||
import android.widget.TextView; |
||||
|
||||
import com.fongmi.bear.databinding.ViewProgressBinding; |
||||
|
||||
import java.util.ArrayList; |
||||
import java.util.Collections; |
||||
import java.util.List; |
||||
|
||||
public class ProgressLayout extends RelativeLayout { |
||||
|
||||
private static final String TAG_PROGRESS = "ProgressLayout.TAG_PROGRESS"; |
||||
private static final String TAG_ERROR = "ProgressLayout.TAG_ERROR"; |
||||
|
||||
public enum State { |
||||
CONTENT, PROGRESS, ERROR |
||||
} |
||||
|
||||
private View mProgressView; |
||||
private TextView mErrorTextView; |
||||
private List<View> mContentViews = new ArrayList<View>(); |
||||
|
||||
private ProgressLayout.State mState = ProgressLayout.State.CONTENT; |
||||
|
||||
public ProgressLayout(Context context) { |
||||
super(context); |
||||
} |
||||
|
||||
public ProgressLayout(Context context, AttributeSet attrs) { |
||||
super(context, attrs); |
||||
init(); |
||||
} |
||||
|
||||
public ProgressLayout(Context context, AttributeSet attrs, int defStyle) { |
||||
super(context, attrs, defStyle); |
||||
init(); |
||||
} |
||||
|
||||
private void init() { |
||||
mProgressView = ViewProgressBinding.inflate(LayoutInflater.from(getContext())).getRoot(); |
||||
LayoutParams layoutParams = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT); |
||||
layoutParams.addRule(CENTER_IN_PARENT); |
||||
mProgressView.setTag(TAG_PROGRESS); |
||||
addView(mProgressView, layoutParams); |
||||
mErrorTextView = new TextView(getContext()); |
||||
mErrorTextView.setTag(TAG_ERROR); |
||||
layoutParams = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT); |
||||
layoutParams.addRule(CENTER_IN_PARENT); |
||||
addView(mErrorTextView, layoutParams); |
||||
mProgressView.setVisibility(GONE); |
||||
} |
||||
|
||||
@Override |
||||
public void addView(View child, int index, ViewGroup.LayoutParams params) { |
||||
super.addView(child, index, params); |
||||
|
||||
if (child.getTag() == null || (!child.getTag().equals(TAG_PROGRESS) && !child.getTag().equals(TAG_ERROR))) { |
||||
mContentViews.add(child); |
||||
} |
||||
} |
||||
|
||||
public void showProgress() { |
||||
switchState(ProgressLayout.State.PROGRESS, null, Collections.<Integer>emptyList()); |
||||
} |
||||
|
||||
public void showProgress(List<Integer> skipIds) { |
||||
switchState(ProgressLayout.State.PROGRESS, null, skipIds); |
||||
} |
||||
|
||||
public void showErrorText() { |
||||
switchState(ProgressLayout.State.ERROR, null, Collections.<Integer>emptyList()); |
||||
} |
||||
|
||||
public void showErrorText(List<Integer> skipIds) { |
||||
switchState(ProgressLayout.State.ERROR, null, skipIds); |
||||
} |
||||
|
||||
public void showErrorText(String error) { |
||||
switchState(ProgressLayout.State.ERROR, error, Collections.<Integer>emptyList()); |
||||
} |
||||
|
||||
public void showErrorText(String error, List<Integer> skipIds) { |
||||
switchState(ProgressLayout.State.ERROR, error, skipIds); |
||||
} |
||||
|
||||
public void showContent() { |
||||
switchState(ProgressLayout.State.CONTENT, null, Collections.<Integer>emptyList()); |
||||
} |
||||
|
||||
public void showContent(List<Integer> skipIds) { |
||||
switchState(ProgressLayout.State.CONTENT, null, skipIds); |
||||
} |
||||
|
||||
public void switchState(ProgressLayout.State state) { |
||||
switchState(state, null, Collections.<Integer>emptyList()); |
||||
} |
||||
|
||||
public void switchState(ProgressLayout.State state, String errorText) { |
||||
switchState(state, errorText, Collections.<Integer>emptyList()); |
||||
} |
||||
|
||||
public void switchState(ProgressLayout.State state, List<Integer> skipIds) { |
||||
switchState(state, null, skipIds); |
||||
} |
||||
|
||||
public void switchState(ProgressLayout.State state, String errorText, List<Integer> skipIds) { |
||||
mState = state; |
||||
switch (state) { |
||||
case CONTENT: |
||||
mErrorTextView.setVisibility(View.GONE); |
||||
mProgressView.setVisibility(View.GONE); |
||||
setContentVisibility(true, skipIds); |
||||
break; |
||||
case PROGRESS: |
||||
mErrorTextView.setVisibility(View.GONE); |
||||
mProgressView.setVisibility(View.VISIBLE); |
||||
setContentVisibility(false, skipIds); |
||||
break; |
||||
case ERROR: |
||||
mErrorTextView.setText(errorText); |
||||
mErrorTextView.setVisibility(View.VISIBLE); |
||||
mProgressView.setVisibility(View.GONE); |
||||
setContentVisibility(false, skipIds); |
||||
break; |
||||
} |
||||
} |
||||
|
||||
public ProgressLayout.State getState() { |
||||
return mState; |
||||
} |
||||
|
||||
public boolean isProgress() { |
||||
return mState == ProgressLayout.State.PROGRESS; |
||||
} |
||||
|
||||
public boolean isContent() { |
||||
return mState == ProgressLayout.State.CONTENT; |
||||
} |
||||
|
||||
public boolean isError() { |
||||
return mState == ProgressLayout.State.ERROR; |
||||
} |
||||
|
||||
private void setContentVisibility(boolean visible, List<Integer> skipIds) { |
||||
for (View v : mContentViews) { |
||||
if (!skipIds.contains(v.getId())) { |
||||
v.setVisibility(visible ? View.VISIBLE : View.GONE); |
||||
} |
||||
} |
||||
} |
||||
} |
||||
@ -0,0 +1,72 @@ |
||||
package com.fongmi.bear.ui.fragment; |
||||
|
||||
import android.os.Bundle; |
||||
import android.view.LayoutInflater; |
||||
import android.view.View; |
||||
import android.view.ViewGroup; |
||||
|
||||
import androidx.annotation.NonNull; |
||||
import androidx.annotation.Nullable; |
||||
import androidx.fragment.app.Fragment; |
||||
import androidx.lifecycle.ViewModelProvider; |
||||
import androidx.recyclerview.widget.GridLayoutManager; |
||||
|
||||
import com.fongmi.bear.databinding.FragmentVodBinding; |
||||
import com.fongmi.bear.model.SiteViewModel; |
||||
import com.fongmi.bear.ui.adapter.VodAdapter; |
||||
import com.fongmi.bear.ui.custom.SpaceItemDecoration; |
||||
|
||||
import java.util.HashMap; |
||||
|
||||
public class VodFragment extends Fragment { |
||||
|
||||
private FragmentVodBinding mBinding; |
||||
private SiteViewModel mSiteViewModel; |
||||
private VodAdapter mVodAdapter; |
||||
|
||||
private String getTypeId() { |
||||
return getArguments().getString("typeId"); |
||||
} |
||||
|
||||
public static VodFragment newInstance(String typeId) { |
||||
Bundle args = new Bundle(); |
||||
args.putString("typeId", typeId); |
||||
VodFragment fragment = new VodFragment(); |
||||
fragment.setArguments(args); |
||||
return fragment; |
||||
} |
||||
|
||||
@Nullable |
||||
@Override |
||||
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { |
||||
mBinding = FragmentVodBinding.inflate(inflater, container, false); |
||||
return mBinding.getRoot(); |
||||
} |
||||
|
||||
@Override |
||||
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) { |
||||
setRecyclerView(); |
||||
setViewModel(); |
||||
getContent(); |
||||
} |
||||
|
||||
private void setRecyclerView() { |
||||
mBinding.recycler.setHasFixedSize(true); |
||||
mBinding.recycler.setLayoutManager(new GridLayoutManager(getContext(), 5)); |
||||
mBinding.recycler.addItemDecoration(new SpaceItemDecoration(5, 12, false, 0)); |
||||
mBinding.recycler.setAdapter(mVodAdapter = new VodAdapter()); |
||||
} |
||||
|
||||
private void setViewModel() { |
||||
mSiteViewModel = new ViewModelProvider(this).get(SiteViewModel.class); |
||||
mSiteViewModel.mResult.observe(getViewLifecycleOwner(), result -> { |
||||
mVodAdapter.addAll(result.getList()); |
||||
mBinding.progress.showContent(); |
||||
}); |
||||
} |
||||
|
||||
private void getContent() { |
||||
mBinding.progress.showProgress(); |
||||
mSiteViewModel.categoryContent(getTypeId(), "1", true, new HashMap<>()); |
||||
} |
||||
} |
||||
@ -0,0 +1,26 @@ |
||||
<?xml version="1.0" encoding="utf-8"?> |
||||
<androidx.core.widget.NestedScrollView xmlns:android="http://schemas.android.com/apk/res/android" |
||||
android:layout_width="match_parent" |
||||
android:layout_height="match_parent" |
||||
android:fillViewport="true"> |
||||
|
||||
<LinearLayout |
||||
android:layout_width="match_parent" |
||||
android:layout_height="match_parent" |
||||
android:orientation="vertical"> |
||||
|
||||
<com.google.android.material.tabs.TabLayout |
||||
android:id="@+id/tab" |
||||
android:layout_width="match_parent" |
||||
android:layout_height="wrap_content" |
||||
android:background="@color/transparent" |
||||
android:nestedScrollingEnabled="false" /> |
||||
|
||||
<androidx.viewpager2.widget.ViewPager2 |
||||
android:id="@+id/pager" |
||||
android:layout_width="match_parent" |
||||
android:layout_height="match_parent" |
||||
android:nestedScrollingEnabled="false" /> |
||||
|
||||
</LinearLayout> |
||||
</androidx.core.widget.NestedScrollView> |
||||
@ -0,0 +1,14 @@ |
||||
<?xml version="1.0" encoding="utf-8"?> |
||||
<com.fongmi.bear.ui.custom.ProgressLayout xmlns:android="http://schemas.android.com/apk/res/android" |
||||
android:id="@+id/progress" |
||||
android:layout_width="match_parent" |
||||
android:layout_height="match_parent" |
||||
android:padding="16dp"> |
||||
|
||||
<androidx.recyclerview.widget.RecyclerView |
||||
android:id="@+id/recycler" |
||||
android:layout_width="match_parent" |
||||
android:layout_height="match_parent" |
||||
android:nestedScrollingEnabled="false" /> |
||||
|
||||
</com.fongmi.bear.ui.custom.ProgressLayout> |
||||
Loading…
Reference in new issue