小编use*_*702的帖子

Android - 在 onCreateViewHolder(..) 中将数据传递给 ViewHolder 构造函数

我有一个RecyclerView,我想将数据传递到RecyclerView.AdapterViewHolder的构造函数onCreateViewHolder(ViewGroup parent, int viewType)

我想要做的是将一个参数(数据项本身)传递给 ViewHolder 的构造函数,这样我就不必在每次单击该项时都检索它。

但是,当我尝试在 onCreateViewHolder(ViewGroup parent, int ViewType) 中检索位置时,我没有得到行的绝对位置,viewType 总是返回 0。

我不想使用 RecyclerView.onItemTouchListener()。

我现在明白我可以使用

    @Override
    public int getItemViewType(int position) {
        return super.getItemViewType(position);
    }
Run Code Online (Sandbox Code Playgroud)

它可以为列表中的每个项目返回唯一的位置。但是我必须编写(冗长的)切换阶梯来处理数据结构中的每一项,这很乏味。

我可以以整数参数返回绝对位置的方式使用 onCreateViewHolder(...) (以便我可以使用该位置将数据传递给 ViewHolder 的构造函数),而不必在 switch 块中单独处理每个项目?

这是我的适配器:

public class Adapter extends RecyclerView.Adapter<Adapter.DataViewHolder> {

    ArrayList<String> mContinentList;
    ClickListener mListener;

    public interface ClickListener{

        void onContinentClicked(String continent);
    }

    public class DataViewHolder extends RecyclerView.ViewHolder{

        TextView mDataItemTextView;
        String mContinent;

        public DataViewHolder(View itemView,String continent) {
            super(itemView);

            mDataItemTextView = (TextView) itemView.findViewById(R.id.data_text_view); …
Run Code Online (Sandbox Code Playgroud)

android android-viewholder android-recyclerview

5
推荐指数
1
解决办法
5032
查看次数

Android - imageView.getDrawable()返回null

我坚持这个看似简单的问题.我究竟做错了什么 ?

ImageView mImageView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mImageView = (ImageView)findViewById(R.id.control);
        ClipDrawable drawable = (ClipDrawable)mImageView.getDrawable();
        drawable.setLevel(10);

    }
Run Code Online (Sandbox Code Playgroud)

应用程序在drawable.setLevel(10)崩溃时出现NullPointerException(drawable为null).

这是我的xml:

circle.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="oval"
    android:innerRadius="50dp"
    >

    <solid
        android:color="#FFAAFF"/>

</shape>
Run Code Online (Sandbox Code Playgroud)

clip.xml

<?xml version="1.0" encoding="utf-8"?>
<clip xmlns:android="http://schemas.android.com/apk/res/android"
    android:clipOrientation="horizontal"
    android:drawable="@drawable/circle"
    android:gravity="clip_horizontal"

/>
Run Code Online (Sandbox Code Playgroud)

activity_main.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:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">

    <ImageView
        android:id="@+id/image"
        android:layout_height="100dp"
        android:layout_width="100dp"
        android:background="@drawable/clip"
        android:layout_centerHorizontal="true"
        />

</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)

请帮帮我吧!

android android-imageview android-drawable

4
推荐指数
1
解决办法
7447
查看次数

使用定时器重复IntentService - 是否可取?

我有一个IntentService从服务器下载数据,我希望IntentService以一定的间隔检查服务器更新.但是以下帖子建议不要重复Service使用a Timer- 而是强调使用AlarmManager:

为什么我的服务不适用于Android?(我只想记录5秒钟的东西)

Android - 服务:只重复一次

Android服务停止

从Android的参考手册中,IntentService被描述为:

IntentService是服务的基类,可根据需要处理异步请求(表示为Intents).客户端通过startService(Intent)调用发送请求; 根据需要启动服务,使用工作线程依次处理每个Intent,并在工作失败时自行停止.

这种"工作队列处理器"模式通常用于从应用程序的主线程卸载任务.存在IntentService类以简化此模式并处理机制.要使用它,请扩展IntentService并实现onHandleIntent(Intent).IntentService将接收Intents,启动工作线程,并根据需要停止服务.

所有请求都在一个工作线程上处理 - 它们可能需要多长时间(并且不会阻止应用程序的主循环),但一次只能处理一个请求.

我不太明白的部分是为什么不允许使用Timer重复执行IntentService(帖子有针对a Service而不是a的问题),IntentService因为它创建了自己的工作线程以供执行.允许在一个Timer内使用IntentService吗?或者是AlarmManagers定期执行的唯一解决方案IntentService

对此的解释将是最受欢迎的.

android android-alarms android-intentservice

3
推荐指数
1
解决办法
4002
查看次数

Android - 无法启动接收器com.parse.ParsePushBroadcastReceiver

我正在尝试使用Parse测试一个非常基本的推送通知程序,但是当我广播通知时我的应用程序崩溃了.这是我的错误日志.

    10-02 23:21:45.625: E/AndroidRuntime(6133): FATAL EXCEPTION: main
    10-02 23:21:45.625: E/AndroidRuntime(6133): java.lang.RuntimeException: Unable to start receiver com.parse.ParsePushBroadcastReceiver: java.lang.NullPointerException
    10-02 23:21:45.625: E/AndroidRuntime(6133):     at android.app.ActivityThread.handleReceiver(ActivityThread.java:2372)
    10-02 23:21:45.625: E/AndroidRuntime(6133):     at android.app.ActivityThread.access$1500(ActivityThread.java:149)
    10-02 23:21:45.625: E/AndroidRuntime(6133):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1376)
    10-02 23:21:45.625: E/AndroidRuntime(6133):     at android.os.Handler.dispatchMessage(Handler.java:99)
    10-02 23:21:45.625: E/AndroidRuntime(6133):     at android.os.Looper.loop(Looper.java:153)
    10-02 23:21:45.625: E/AndroidRuntime(6133):     at android.app.ActivityThread.main(ActivityThread.java:4987)
    10-02 23:21:45.625: E/AndroidRuntime(6133):     at java.lang.reflect.Method.invokeNative(Native Method)
    10-02 23:21:45.625: E/AndroidRuntime(6133):     at java.lang.reflect.Method.invoke(Method.java:511)
    10-02 23:21:45.625: E/AndroidRuntime(6133):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:821)
    10-02 23:21:45.625: E/AndroidRuntime(6133):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:584)
    10-02 23:21:45.625: E/AndroidRuntime(6133):     at dalvik.system.NativeStart.main(Native Method)
    10-02 23:21:45.625: E/AndroidRuntime(6133): Caused by: java.lang.NullPointerException …
Run Code Online (Sandbox Code Playgroud)

android push-notification parse-platform

3
推荐指数
1
解决办法
3490
查看次数

Android-在特定活动中更改主题

我的应用程序的基本主题是:

<style name="AppTheme" parent="@android:style/Theme.Holo.Light">
        <item name="android:actionBarStyle">@style/MyActionBar</item>
        <item name="android:actionBarTabTextStyle">@style/MyActionBarTabText</item>
        <item name="android:actionBarTabBarStyle">@style/MyActionBarTabBar</item>
        <item name="android:actionBarTabStyle">@style/MyActionBarTabView</item>
        <item name="android:popupMenuStyle">@style/MyMenu</item>
        <!--<item name="android:actionBarStyle">@style/window_action_bar_style</item>-->
        <item name="android:layout_width">wrap_content</item>
        <item name="android:layout_height">wrap_content</item>
        <item name="android:windowContentOverlay">@null</item>
    </style>
Run Code Online (Sandbox Code Playgroud)

我想将这个主题应用于一项活动.

<style name="AppTheme" parent="Theme.AppCompat.Light">
</style>
Run Code Online (Sandbox Code Playgroud)

但是,当我这样做时,我无法访问该活动中的SwitchCompat小部件:

<android.support.v7.widget.SwitchCompat/>
Run Code Online (Sandbox Code Playgroud)

我在这做错了什么?任何建议或解决方案将是最受欢迎的.当我尝试覆盖特定活动中的基本主题并尝试访问SwitchCompat小部件时,会发生此问题.我的项目已经非常大,我可以更改整个应用程序的基本主题,因为我将不得不对ActionBars和其他视图进行更改.怎么解决这个问题?

android android-theme android-switch

3
推荐指数
1
解决办法
5195
查看次数

Android - 相当于UITableViewCell?

在Android中是否有相当于UITableViewCella RecyclerView或(任何其他可回收视图)?我的搜索都没有给我任何指示.一些帮助将非常感激.

我知道a RecyclerView的位置对应于列表中的行.但这是正确的,这相当于UITableViewCellAndroid吗?

android listview android-recyclerview

1
推荐指数
1
解决办法
613
查看次数