RecyclerView问题:仅在手机处于纵向状态时填充数据,但在横向模式下填充

时间:2016-12-14 10:25:55

标签: android android-recyclerview android-scrollview

我正面临着一个非常奇怪的问题。当我使用纵向模式时,数据不会填充RecyclerView,而是填充横向模式。 但是,当我使用模拟器尝试它时,它在两个屏幕方向都能正常工作。

清单文件:

 <activity android:name="user.com.hlthee.MainActivity"
            android:configChanges="keyboardHidden|orientation|screenSize"
            />

XML文件:

 <LinearLayout
                android:orientation="vertical"
                android:weightSum="2"
                android:layout_width="match_parent"
                android:layout_height="wrap_content">

                <android.support.v7.widget.CardView
                    app:cardElevation="24dp"
                    android:elevation="24dp"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content">

                    <RelativeLayout
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content">

                        <TextView
                            android:id="@+id/adherence_text"
                            android:textStyle="bold"
                            android:layout_margin="5dp"
                            android:textAlignment="center"
                            android:textAppearance="@style/TextAppearance.AppCompat.Medium"
                            android:textColor="@color/primary_text"
                            android:text="Adherence"
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content" />

                        <android.support.v7.widget.RecyclerView
                            android:layout_marginStart="@dimen/activity_horizontal_margin"
                            android:layout_below="@+id/adherence_text"
                            android:id="@+id/action_with_adherence"
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content">

                        </android.support.v7.widget.RecyclerView>

                    </RelativeLayout>

                </android.support.v7.widget.CardView>

            </LinearLayout>

这是我的java文件: -

actionWithAdherence=(RecyclerView) findViewById(R.id.action_with_adherence);

        actionWithAdherence.setLayoutManager(new GridAutofitLayoutManager(this,200));

        ArrayList<Action> actionListWithAdherence=database.fetchDataFromAdherenceTable();

 actionWithAdherence.setAdapter(new DashboardAdherence(this, actionListWithAdherence));

尝试解决方案:

添加android:configChanges="keyboardHidden|orientation|screenSize" 但没有帮助。我还从互联网上读到,保存和恢复savedInstanceState可能有所帮助。但我认为这只是为了挽救国家(即移居到另一个国家之前的状态是什么activity。)

编辑: -

适配器类:

public class DashboardAdherence extends RecyclerView.Adapter<DashboardAdherence.AdherenceHolder> {
List<Action> actionList;
Context context;
private View layoutInflater;

public DashboardAdherence(Context context, List<Action> actionList)
{
    this.context=context;
    this.actionList=actionList;
}

@Override
public int getItemCount() {
    System.out.println("Size is "+actionList.size());
    return 9;
}

@Override
public AdherenceHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
    View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.adherence_dashboard, viewGroup, false);
    return new AdherenceHolder(view);
}

@Override
public void onBindViewHolder(AdherenceHolder adherenceHolder, int position) {

    Action action=actionList.get(position);

    adherenceHolder.actionName.setText(action.getAction_id());

    adherenceHolder.actionAdherence.setValue(action.getUserPercentage());
    adherenceHolder.actionAdherence.setText("55");
    adherenceHolder.actionAdherence.setUnitVisible(true);


    //circleProgressView[i].setBarColor(getResources().getColor(R.color.green));

}


public class AdherenceHolder extends RecyclerView.ViewHolder {


    private CircleProgressView actionAdherence;
    private TextView actionName;

    public AdherenceHolder(View view)
    {
        super(view);

        actionAdherence=(CircleProgressView) itemView.findViewById(R.id.action_adherence);
        actionName=(TextView) itemView.findViewById(R.id.action_name);
    }
}
}

MainActivity.java

public class MainActivity extends AppCompatActivity implements HealthVitalsFunction {

   SharedPreferences app_preferences;   
    RecyclerView actionWithAdherence;  
   DbHelper database;


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



        //set basic settings for app.
        app_preferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
        Constants.setTokenDB(app_preferences.getString("token", ""));
        database = new DbHelper(MainActivity.this);



        /*-------------------------------- Action with the Adherence------------------------*/

        actionWithAdherence=(RecyclerView) findViewById(R.id.action_with_adherence);
        //second parameter 3 represents it has 4 column
        actionWithAdherence.setLayoutManager(new GridLayoutManager(this, 4));
        //actionWithAdherence.setLayoutManager(new GridAutofitLayoutManager(this,200));

        ArrayList<Action> actionListWithAdherence=database.fetchDataFromAdherenceTable();

        actionWithAdherence.setAdapter(new DashboardAdherence(this, actionListWithAdherence));

}


    @Override
    protected void onResume() {
        super.onResume();

     }


    @Override
    public void onBackPressed() {

        if (exit) {
            finish();
        } else {

            Toast.makeText(this, "Press Back again to Exit.", Toast.LENGTH_SHORT).show();
            exit = true;

            new Handler().postDelayed(new Runnable() {
                @Override
                public void run() {
                    exit = false;
                }
            }, 3000);
        }

        super.onBackPressed();

    }

}

的build.gradle(APP):

apply plugin: 'com.android.application'

android {
    compileSdkVersion 24
    buildToolsVersion "24.0.3"

   /* splits {
        abi {
            enable true
            reset()
            include 'x86', 'x86_64', 'armeabi', 'armeabi-v7a', 'arm64-v8a', 'mips'
            universalApk false
        }
    }*/
    defaultConfig {
        applicationId "user.com.hlthee"
        minSdkVersion 17
        targetSdkVersion 24
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        /*release {
            debuggable false
            minifyEnabled true
            shrinkResources true
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
        debug {
            debuggable true
            minifyEnabled true
            shrinkResources true
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }*/
    }
    sourceSets {
        main {
            assets.srcDirs = ['src/main/assets', 'src/main/assets/']
            res.srcDirs = ['src/main/res', 'src/main/assets/fonts']
        }
    }

    dexOptions {
        jumboMode true
    }

    packagingOptions {
        exclude 'META-INF/DEPENDENCIES'
        exclude 'META-INF/NOTICE'
        exclude 'META-INF/LICENSE'
        exclude 'META-INF/LICENSE.txt'
        exclude 'META-INF/NOTICE.txt'
        exclude 'META-INF/ECLIPSE_.SF'
        exclude 'META-INF/ECLIPSE_.RSA'
    }
}


dependencies {
    compile fileTree(include: ['*.jar'], dir: 'libs')
    testCompile 'junit:junit:4.12'
    //for bottom bar
    compile 'com.roughike:bottom-bar:2.0.2'

    compile 'com.android.support:cardview-v7:24.0.0'
    compile 'com.android.support:recyclerview-v7:24.2.0'
    compile 'com.android.support:appcompat-v7:24.2.0'
    compile 'com.android.support:design:24.2.0'
    //for splash screen
    compile 'com.github.ViksaaSkool:AwesomeSplash:v1.0.0'
    //for volley
    compile 'com.android.volley:volley:1.0.0'
    /*compile 'com.mcxiaoke.volley:library-aar:1.0.0'*/
   // compile 'com.nineoldandroids:library:2.4.0'
    /*compile 'de.hdodenhof:circleimageview:2.1.0'*/
    compile 'com.daimajia.easing:library:1.0.1@aar'
    compile 'com.daimajia.androidanimations:library:1.1.3@aar'
    compile 'com.mikepenz:iconics-core:2.6.7@aar'
    compile 'com.github.jjobes:SlideDateTimePicker:v1.0.4'
    //graph library
    compile 'com.github.lecho:hellocharts-android:v1.5.8'
    compile('com.mikepenz:materialdrawer:5.3.6@aar') {
        transitive = true
    }
    compile 'com.mikepenz:google-material-typeface:2.2.0.2.original@aar'
    //Google Material Icons
    compile 'com.mikepenz:fontawesome-typeface:4.6.0.2@aar'
    /*compile 'com.jjoe64:graphview:4.2.0'*/
    compile files('libs/commons-io-2.5.jar')
    //compile files('libs/PDFRenderer-0.9.0.jar')
    //To show the thumbnail of pdf
    compile 'com.github.barteksc:pdfium-android:1.4.0'
    //Comment this line before production
    compile 'com.facebook.stetho:stetho:1.4.1'
    //library for chart
    compile 'com.github.lecho:hellocharts-android:v1.5.8'
    //library for timelineView
    compile 'com.github.alorma:timelineview:2.3.0'
    //for constraint layout
    //compile 'com.android.support.constraint:constraint-layout:1.0.0-beta1'
    //for circular view
    //compile 'com.mikhaellopez:circularimageview:3.0.2'
    //for applozic chat api
    compile project(':mobicomkitui')
    compile 'com.google.android.gms:play-services-appindexing:9.0.2'
    //for junit
    testCompile 'junit:junit:4.12'
    //for mockito(framework for making test writing process)
    //testCompile 'org.mockito:mockito-core:1.10.19'
    //for the curved image view . use in the goal
    compile 'com.github.developer-shivam:crescento:1.0.0'
    //for animated image views
    compile 'com.flaviofaria:kenburnsview:1.0.7'
    //universal image loader to load the image.
    compile 'com.github.bumptech.glide:glide:3.7.0'

    compile 'de.hdodenhof:circleimageview:1.3.0'

    //for adherence progress circular bar
    compile 'com.github.jakob-grabner:Circle-Progress-View:v1.2.9.1'

    //for the sectioned recycler view
    compile 'com.github.IntruderShanky:Sectioned-RecyclerView:2.1.1'
}

apply plugin: 'com.google.gms.google-services'
apply plugin: 'com.getkeepsafe.dexcount'

fetchDataFromAdherenceTable()方法。

public ArrayList<Action> fetchDataFromAdherenceTable() {

        SQLiteDatabase sqLiteDatabase=this.getReadableDatabase();

        ArrayList<Action> actionList=new ArrayList<>();

        Cursor cursor=sqLiteDatabase.rawQuery(" select * from "+adherenceActionCOlumnTableName, null);

        cursor.moveToFirst();
        while (!cursor.isAfterLast()){

            Action action=new Action();

            action.setAction_id(cursor.getString(cursor.getColumnIndex(adherenceActionCOlumn_id)));
            action.setUserPercentage(cursor.getInt(cursor.getColumnIndex(adherenceActionCOlumn_adherence)));

            actionList.add(action);
            cursor.moveToNext();
        }

        cursor.close();
        return actionList;

    }

适配器xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:layout_margin="@dimen/activity_horizontal_margin"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <at.grabner.circleprogress.CircleProgressView
        app:cpv_maxValue="100"
        app:cpv_textSize="15dp"
        app:cpv_textColor="@color/white"
        app:cpv_unit="%"
        app:cpv_unitSize="10dp"
        app:cpv_unitColor="@color/white"
        app:cpv_unitScale="1"
        app:cpv_rimColor="@color/secondary_background"
        app:cpv_rimWidth="10dp"
            app:cpv_barWidth="4dp"
            android:id="@+id/action_adherence"
            android:layout_width="50dp"
            android:layout_height="50dp" />

    <TextView
        android:id="@+id/action_name"
            android:textAppearance="@style/Base.TextAppearance.AppCompat.Small"
            android:layout_marginTop="5dp"
            android:text="Exercise"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />

</LinearLayout>

0 个答案:

没有答案