Android TV中实现RecyView循环功能
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Android TV中实现RecyView循环功能相关的知识,希望对你有一定的参考价值。
参考技术A RecycleView 用法 :1. [endif]在Gradle中添加
implementation 'com.android.support:recyclerview-v7:26.0.2'
2. [endif]在主布局文件里添加RecycleView
3. [endif]创建item布局文件
在电视开发中,需要添加focusable 与 focusableintouchmode 才可以获得焦点。
4. [endif]创建adapter继承RecyclerView.Adapter
5. [endif] 在adapter中添加缺省方法
1) [endif]On create ViewHolder()方法是将item布局加载到recycleView 的方法
View view = LayoutInflater. from (parent.getContext()).inflate(R.layout. item_2 , parent, false);
创建一个ViewHolder,并添加到ViewHolder里面去,最后返回这个ViewHolder:
ViewHolder holder = new ViewHolder(view);return holder;
2) OnbindViewHolder()是将ViewHolder绑定到RecycleView上面时调用的方法
可在这里做更新View的操作。
3) [endif]GetItemCount 方法为返回数据长度的方法。一般返回值为:
return XXX.size();
4) [endif]在adapter中创建一个viewHolder,继承
RecyclerView.ViewHolder
在这里创建item布局中的控件对象,在public ViewHolder(View itemView)
方法中初始化:
tvItem2 = itemView.findViewById(R.id. tv_item_2 );
6. 在adapter 中创建一个数据对象用来接收想要传入的数据,在结构体中接收传入的数据例如:
private List mData;
在Activity中的操作
1. [endif]创建recycleView对象,创建layoutManager(布局管理器)对象,创建adapter对象,创建数据对象
2. 实例化recycleView对象,layoutManager对象,adapter对象
RecycleView有几种布局管理器可用,可实现线性,网格,以及瀑布流的界面,以下为线性布局:
mlayoutManager = new LinearLayoutManager(this);
以下为网格布局:
//mlayoutManager = new GridLayoutManager(this,3,GridLayoutManager.HORIZONTAL,false);
3.为recycleview 设置布局管理器:
viewRecy2.setLayoutManager(mlayoutManager);
4.为recycleview设置adapter:
//3 设置数据适配器viewRecy2.setAdapter(myAdapter);
如果要在每一个item中添加事件,采取事件回调的方法,其他自定义View的回调皆类似,以下以添加点击事件为例:
1) 在adapter中添加点击事件的接口,并添加接口方法:
public interface onItemClickListener void onItemClick(View view, int position);
2) [endif]在adapter中创建接口实例
private onItemClickListener onItemClickListener;
3) 在adapter创建对应set方法体来接收Activity传入的对象
public void setOnItemClickListener(onItemClickListener onItemClickListener) this.onItemClickListener = onItemClickListener;
4)在onbindViewholde中判断是否设置了监听器
if (onItemClickListener !=null)
//为ItemView设置监听器
holder.tvItem2.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View v)
int position =holder.getLayoutPosition();
onItemClickListener.onItemClick(holder.tvItem2, position);
//onItemClickListener.onItemClick();
);
5) 最后在activity中通过adapter对象调用onset方法,实现点击事件
myAdapter.setOnItemClickListener(new MyAdapter.onItemClickListener()
@Override
public void onItemClick(View view, int position)
lastPosition =currentPosition;
currentPosition = position;
// Toast.makeText(RecycleActivity_2.this, "click+" + position, Toast.LENGTH_SHORT).show();
myAdapter.notifyItemChanged(currentPosition);
myAdapter.notifyItemChanged(lastPosition);
);
Android TV中RecycleView的Item循环实现
(1) 在TV中遥控器的按键取代了手机的滑动,因此我们要监听遥控器的按键,因此在adapter中的item监听setonkeylistener方法。
并获取item所在位置的position,存到tag里面
holder.tvItem2.setTag(position);
当在第一个Item中按向上键时,position<0,此时我们调用移动到最后一个,同理,当在最后一个时,移动到第一个。
smoothScrollToPosition(getItemCount() - 1)
holder.tvItem2.setOnKeyListener(new View.OnKeyListener()
@Override
public boolean onKey(View v, int keyCode, KeyEvent event)
int action = event.getAction();
// Log.e("---", keyCode + "");
switch (keyCode)
case KeyEvent.KEYCODE_DPAD_UP:
int positionUp = (int) v.getTag();
if (action == KeyEvent.ACTION_DOWN)
if (positionUp <=0)
recyclerView.smoothScrollToPosition(getItemCount() -1);
return true;
break;
case KeyEvent.KEYCODE_DPAD_DOWN:
int positionDown = (int) v.getTag();
if (action == KeyEvent.ACTION_DOWN)
if (positionDown >= getItemCount() -1)
recyclerView.smoothScrollToPosition(0);
return true;
break;
return false;
);
(2) 然后在activity中封装了一个回滚方法来判断按键是否到达底部,并重新获取焦点
public void scroolChange(RecyclerView recyclerView, boolean toFirst, boolean toLast)
LinearLayoutManager manager = (LinearLayoutManager) recyclerView.getLayoutManager();
int lastVisibleItem = manager.findLastCompletelyVisibleItemPosition();
int firstVisibleItem = manager.findFirstCompletelyVisibleItemPosition();
int totalItemCount = manager.getItemCount();
//向下滚动,到底部
if (lastVisibleItem == (totalItemCount -1) && toLast)
View view = recyclerView.getChildAt(lastVisibleItem);
LinearLayoutManager llM = (LinearLayoutManager) recyclerView.getLayoutManager();
if (view !=null)
view.requestFocus();
else if (llM.findViewByPosition(lastVisibleItem) !=null)
llM.findViewByPosition(lastVisibleItem).requestFocus();
else
recyclerView.requestFocus();
//向上滚动,到顶部
if (firstVisibleItem ==0 && toFirst)
View view = recyclerView.getChildAt(firstVisibleItem);
LinearLayoutManager llM = (LinearLayoutManager) recyclerView.getLayoutManager();
if (view !=null)
view.requestFocus();
else if (llM.findViewByPosition(firstVisibleItem) !=null)
llM.findViewByPosition(firstVisibleItem).requestFocus();
else
recyclerView.requestFocus();
(3) 最后在recycleView 调用addscrolllistener方法,来监听recycleView的滚动状态
由于setonScrollListener有时回产生空指针,因此建议使用add方法
viewRecy2.addOnScrollListener(new RecyclerView.OnScrollListener()
boolean toLast =false;
boolean toFirst =false;
@Override
public void onScrollStateChanged(RecyclerView recyclerView, int newState)
super.onScrollStateChanged(recyclerView, newState);
if (newState == recyclerView.SCROLL_STATE_IDLE)
// Log.e("---", "底部");
scroolChange(recyclerView, toFirst, toLast);
@Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy)
super.onScrolled(recyclerView, dx, dy);
if (dy >0)
toLast =true;
else
toFirst =true;
);
Add中包含两个方法,都可以来判断是否到达顶部或底部。至此,实现了Android TV中RecycleView的循环功能。
android 中实现网页调用摄像头功能?怎么实现?
android 中实现网页调用摄像头功能方法·如下:1. 首先在Manifest.xml中添加如下权限:
<!-- 访问网络权限 -->
<uses-permission android:name="android.permission.INTERNET"/>
<!-- 调用摄像头的权限 -->
<uses-permission android:name="android.permission.CAMERA"/>
<!-- 文件系统权限 -->
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/>
<!-- SD卡写权限 -->
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
2. 编写index.html放在android项目下的assests目录下:
index.html:
<html>
<head>
<title>Webview capture testing...</title>
<script type="text/javascript">
//android中调用js方法
function wave()
alert('cc2');
document.getElementById("droid").innerHTML ="android_waving.png";
//android中调用js方法2,并更id为"droid"的span标签的值及新刚拍的照片显示到"pic0"里
function wave2(name)
alert(name);
document.getElementById("droid").innerHTML = "Photopath: " + name;
document.getElementById("pic0").src=name;
//html的点击事件,并通过"window.demo.clickOnAndroid"触发android中的clickOnAndroid方法
function clickOnHtml ()
var fileName = window.demo.clickOnAndroid();
//wave2(fileName);
//
function revert()
alert('cc');
document.getElementById("droid").innerHTML ="aa2";
</script>
<style type="text/css">
body
font:14px;
color:#333;
line-height: 1.8em;
margin: 0;
padding: 0;
width:480px;
html
outline: none;
hide-focus:expression(this.hideFocus=true);
</style>
</head>
<body>
<div>
<li>
<p>Here is the funtion to TakePhoto in html</p>
<!-- 通过"window.demo.clickOnAndroid"触发android中的clickOnAndroid方法 -->
<a onClick="window.demo.clickOnAndroid()">TakePhoto</a><br/>
<span id='droid'>Photopath:</span>
<br/>
<br/>
<p>Here is the funtion to revert in html</p>
<input type="button" onclick="revert();" value="Revert" /> <br/>
<input type="button" onclick="javascript:window.top.location.reload()" value="Refresh" />
</li>
</div>
<div>
<p id="picpath">PhotoPath</p>
<img alt="ddd" id="pic0" width="200" height="200" src="/storage/sdcard0/webview_camera/testimg23.8156950140954181.jpg" />
</div>
</body>
</html>
3、编写Activity的布局activity_camera_webview.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".CameraWebviewActivity" >
<Button
android:id="@+id/bt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Camera"
android:layout_alignParentTop="true"
/>
<WebView
android:id="@+id/wv"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:layout_below="@+id/bt"
/>
</RelativeLayout>
4.、编写activity:
public class CameraWebviewActivity extends Activity
private final static String TAG = "CameraWebviewActivity";
private Button bt;
private WebView wv;
public String fileFullName;//照相后的照片的全整路径
private boolean fromTakePhoto; //是否是从摄像界面返回的webview
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_camera_webview);
initViews();
private void initViews()
bt = (Button) findViewById(R.id.bt);
bt.setOnClickListener(new OnClickListener()
@Override
public void onClick(View v)
System.out.println("----------------");
takePhoto( Math.random()*1000+1 + ".jpg");
);
wv = (WebView) findViewById(R.id.wv);
WebSettings setting = wv.getSettings();
setting.setJavaScriptEnabled(true);
wv.setWebViewClient(new WebViewClient()
@Override
public void onPageStarted(WebView view, String url, Bitmap favicon)
super.onPageStarted(view, url, favicon);
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url)
return super.shouldOverrideUrlLoading(view, url);
@Override
public void onPageFinished(WebView view, String url)
super.onPageFinished(view, url);
);
wv.setWebChromeClient(new WebChromeClient()
@Override//实现js中的alert弹窗在Activity中显示
public boolean onJsAlert(WebView view, String url, String message, JsResult result)
Log.d(TAG, message);
result.confirm();
return true;
);
wv.loadUrl("file:///android_asset/index.html");
final Handler mHandler = new Handler();
//webview增加javascript接口,监听html页面中的js点击事件
wv.addJavascriptInterface(new Object()
public String clickOnAndroid() //将被js调用
mHandler.post(new Runnable()
public void run()
fromTakePhoto = true;
//调用 启用摄像头的自定义方法
takePhoto("testimg" + Math.random()*1000+1 + ".jpg");
System.out.println("========fileFullName: " + fileFullName);
);
return fileFullName;
, "demo");
/*
* 调用摄像头的方法
*/
public void takePhoto(String filename)
System.out.println("----start to take photo2 ----");
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_MEDIA_TITLE, "TakePhoto");
//判断是否有SD卡
String sdDir = null;
boolean isSDcardExist = Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED);
if(isSDcardExist)
sdDir = Environment.getExternalStorageDirectory().getAbsolutePath();
else
sdDir = Environment.getRootDirectory().getAbsolutePath();
//确定相片保存路径
String targetDir = sdDir + "/" + "webview_camera";
File file = new File(targetDir);
if (!file.exists())
file.mkdirs();
fileFullName = targetDir + "/" + filename;
System.out.println("----taking photo fileFullName: " + fileFullName);
//初始化并调用摄像头
intent.putExtra(MediaStore.Images.Media.MIME_TYPE, "image/jpeg");
intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(new File(fileFullName)));
startActivityForResult(intent, 1);
/*
* (non-Javadoc)
* @see android.app.Activity#onActivityResult(int, int, android.content.Intent)
* 重写些方法,判断是否从摄像Activity返回的webview activity
*/
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
System.out.println("----requestCode: " + requestCode + "; resultCode " + resultCode + "; fileFullName: " + fileFullName);
if (fromTakePhoto && requestCode ==1 && resultCode ==-1)
wv.loadUrl("javascript:wave2('" + fileFullName + "')");
else
wv.loadUrl("javascript:wave2('Please take your photo')");
fromTakePhoto = false;
super.onActivityResult(requestCode, resultCode, data);
@Override
public boolean onCreateOptionsMenu(Menu menu)
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.camera_webview, menu);
return true;
参考技术A 页面上做一个按钮,如打开摄像头,它做的事情就是将页面链接到一个特殊的串,如<a href=opencapture打开摄像头</a使用安卓的webview访问网页页面,同时重写shouldOverrideUrlLoading捕获url,当判断url为opencapture时,调用系统摄像头本回答被提问者采纳
以上是关于Android TV中实现RecyView循环功能的主要内容,如果未能解决你的问题,请参考以下文章
尝试提交查询时 Android TV SearchFragment 崩溃(NPE)