Skip to content

Commit

Permalink
add demo中增加了在tv中通过遥控器控制切换的方法
Browse files Browse the repository at this point in the history
Change-Id: I03fa8c717ebb70d13b88f1cb58953d6d42ae0911
  • Loading branch information
youth5201314 committed Apr 9, 2020
1 parent f771d15 commit 6d883ff
Show file tree
Hide file tree
Showing 8 changed files with 118 additions and 12 deletions.
1 change: 0 additions & 1 deletion app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ dependencies {
implementation 'com.shuyu:GSYVideoPlayer:7.1.3'



implementation project(':banner')
implementation 'org.jetbrains:annotations:15.0'
// implementation 'com.youth.banner:banner:+'
Expand Down
7 changes: 3 additions & 4 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.test.banner">
<uses-permission android:name="android.permission.INTERNET" />

<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme">

<activity android:name=".ui.TVActivity"></activity>
<activity
android:name=".MainActivity"
android:label="@string/app_name">
Expand All @@ -20,12 +19,12 @@
</activity>
<activity android:name=".ui.RecyclerViewBannerActivity" />
<activity android:name=".ui.ConstraintLayoutBannerActivity" />
<activity android:name=".ui.VideoActivity"/>
<activity android:name=".ui.VideoActivity" />
<activity
android:name=".ui.Vp2FragmentRecyclerviewActivity"
android:theme="@style/Theme.AppCompat.Light.NoActionBar" />
</application>


<uses-permission android:name="android.permission.INTERNET" />

</manifest>
7 changes: 5 additions & 2 deletions app/src/main/java/com/test/banner/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import com.test.banner.bean.DataBean;
import com.test.banner.ui.ConstraintLayoutBannerActivity;
import com.test.banner.ui.RecyclerViewBannerActivity;
import com.test.banner.ui.TVActivity;
import com.test.banner.ui.VideoActivity;
import com.test.banner.ui.Vp2FragmentRecyclerviewActivity;
import com.youth.banner.Banner;
Expand Down Expand Up @@ -69,7 +70,6 @@ protected void onCreate(Bundle savedInstanceState) {
//圆角
banner.setBannerRound(BannerUtils.dp2px(5));


//添加画廊效果,可以参考我给的参数自己调试(不要和其他PageTransformer同时使用)
// banner.setBannerGalleryEffect(25, 40, 0.14f);

Expand Down Expand Up @@ -136,7 +136,7 @@ protected void onStop() {

@OnClick({R.id.style_image, R.id.style_image_title, R.id.style_image_title_num, R.id.style_multiple,
R.id.style_net_image, R.id.change_indicator, R.id.rv_banner, R.id.cl_banner, R.id.vp_banner,
R.id.banner_video})
R.id.banner_video,R.id.banner_tv})
public void click(View view) {
indicator.setVisibility(View.GONE);
switch (view.getId()) {
Expand Down Expand Up @@ -191,6 +191,9 @@ public void click(View view) {
case R.id.banner_video:
startActivity(new Intent(this, VideoActivity.class));
break;
case R.id.banner_tv:
startActivity(new Intent(this, TVActivity.class));
break;
}
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package com.test.banner.ui;

import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;

import androidx.appcompat.app.AppCompatActivity;

Expand All @@ -14,6 +16,7 @@
import butterknife.ButterKnife;

public class ConstraintLayoutBannerActivity extends AppCompatActivity {
private static final String TAG = "banner_log";
@BindView(R.id.banner)
Banner banner;

Expand All @@ -24,9 +27,9 @@ protected void onCreate(Bundle savedInstanceState) {
ButterKnife.bind(this);
banner.setAdapter(new ImageAdapter(DataBean.getTestData()));
banner.setIndicator(new CircleIndicator(this));
banner.isAutoLoop(false);
}


@Override
protected void onStart() {
super.onStart();
Expand Down
66 changes: 66 additions & 0 deletions app/src/main/java/com/test/banner/ui/TVActivity.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package com.test.banner.ui;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;

import com.test.banner.R;
import com.test.banner.adapter.ImageAdapter;
import com.test.banner.bean.DataBean;
import com.youth.banner.Banner;
import com.youth.banner.indicator.CircleIndicator;
import com.youth.banner.util.BannerUtils;

import butterknife.BindView;
import butterknife.ButterKnife;

public class TVActivity extends AppCompatActivity {

private static final String TAG = "banner_log";
@BindView(R.id.banner)
Banner banner;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_t_v);
ButterKnife.bind(this);
banner.setAdapter(new ImageAdapter(DataBean.getTestData()));
banner.setIndicator(new CircleIndicator(this));
banner.isAutoLoop(false);
}

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
int count = banner.getItemCount();
switch (keyCode) {
case KeyEvent.KEYCODE_DPAD_LEFT:
Log.d(TAG, "向左");
int prev = (banner.getCurrentItem() - 1) % count;
if (prev == 0) {
prev = banner.getRealCount();
} else if (prev == count - 1) {
prev = 1;
}
banner.setCurrentItem(prev, false);
break;
case KeyEvent.KEYCODE_DPAD_RIGHT:
Log.d(TAG, "向右");
int next = (banner.getCurrentItem() + 1) % count;
if (next == 0) {
next = banner.getRealCount();
} else if (next == count - 1) {
next = 1;
}
banner.setCurrentItem(next, false);
break;
}
//如果没有设置指示器,就不用执行下面两行
int real = BannerUtils.getRealPosition(banner.isInfiniteLoop(), banner.getCurrentItem(), banner.getRealCount());
banner.getIndicator().onPageSelected(real);
return super.onKeyDown(keyCode, event);
}

}
7 changes: 7 additions & 0 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,13 @@
android:textAllCaps="false"
android:text="仿淘宝商品详情banner,第一个放视频"
android:layout_below="@+id/vp_banner"/>
<Button
android:id="@+id/banner_tv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAllCaps="false"
android:text="测试在tv中,遥控切换banner"
android:layout_below="@+id/banner_video"/>
</RelativeLayout>
</androidx.core.widget.NestedScrollView>
</androidx.swiperefreshlayout.widget.SwipeRefreshLayout>
29 changes: 29 additions & 0 deletions app/src/main/res/layout/activity_t_v.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".ui.TVActivity">

<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="15dp"
android:text="TV中遥控控制banner切换,因为没有真机测试,所以不确定有没有问题,有测试环境的朋友可以多多反馈,或者修改哈"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

<com.youth.banner.Banner
android:id="@+id/banner"
android:layout_width="0dp"
android:layout_height="0dp"
app:indicator_selected_color="@color/colorPrimary"
app:layout_constraintDimensionRatio="3:1"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@id/textView" />
</androidx.constraintlayout.widget.ConstraintLayout>
8 changes: 4 additions & 4 deletions banner/src/main/java/com/youth/banner/Banner.java
Original file line number Diff line number Diff line change
Expand Up @@ -349,19 +349,19 @@ public void onChanged() {
}
};

private void setCurrentItem(int position) {
public void setCurrentItem(int position) {
setCurrentItem(position, true);
}

private void setCurrentItem(int position, boolean smoothScroll) {
public void setCurrentItem(int position, boolean smoothScroll) {
mViewPager2.setCurrentItem(position, smoothScroll);
}

private int getCurrentItem() {
public int getCurrentItem() {
return mViewPager2.getCurrentItem();
}

private int getItemCount() {
public int getItemCount() {
if (getAdapter() == null)
return 0;
return getAdapter().getItemCount();
Expand Down

0 comments on commit 6d883ff

Please sign in to comment.