Skip to content
This repository has been archived by the owner on Oct 3, 2020. It is now read-only.

Commit

Permalink
[ADDED] Initial commit with working demo for the workaround.
Browse files Browse the repository at this point in the history
  • Loading branch information
hossain-khan committed Aug 17, 2016
1 parent 3ec124d commit dc0a997
Show file tree
Hide file tree
Showing 45 changed files with 1,143 additions and 0 deletions.
32 changes: 32 additions & 0 deletions app/build.gradle
@@ -0,0 +1,32 @@
apply plugin: 'com.android.application'

android {
compileSdkVersion 24
buildToolsVersion "24.0.1"
defaultConfig {
applicationId "info.hossainkhan.recyclerviewdemo"
minSdkVersion 14
targetSdkVersion 24
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}

dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
exclude group: 'com.android.support', module: 'support-annotations'
})
compile 'com.android.support:appcompat-v7:24.1.1'
compile 'com.android.support:support-v4:24.1.1'
compile 'com.android.support:design:24.1.1'
compile 'com.android.support.constraint:constraint-layout:1.0.0-alpha4'
testCompile 'junit:junit:4.12'
}
17 changes: 17 additions & 0 deletions app/proguard-rules.pro
@@ -0,0 +1,17 @@
# Add project specific ProGuard rules here.
# By default, the flags in this file are appended to flags specified
# in /Users/hossain/development/android-sdk-macosx/tools/proguard/proguard-android.txt
# You can edit the include path and order by changing the proguardFiles
# directive in build.gradle.
#
# For more details, see
# http://developer.android.com/guide/developing/tools/proguard.html

# Add any project specific keep options here:

# If your project uses WebView with JS, uncomment the following
# and specify the fully qualified class name to the JavaScript interface
# class:
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
# public *;
#}
@@ -0,0 +1,26 @@
package info.hossainkhan.recyclerviewdemo;

import android.content.Context;
import android.support.test.InstrumentationRegistry;
import android.support.test.runner.AndroidJUnit4;

import org.junit.Test;
import org.junit.runner.RunWith;

import static org.junit.Assert.*;

/**
* Instrumentation test, which will execute on an Android device.
*
* @see <a href="http://d.android.com/tools/testing">Testing documentation</a>
*/
@RunWith(AndroidJUnit4.class)
public class ExampleInstrumentedTest {
@Test
public void useAppContext() throws Exception {
// Context of the app under test.
Context appContext = InstrumentationRegistry.getTargetContext();

assertEquals("info.hossainkhan.recyclerviewdemo", appContext.getPackageName());
}
}
23 changes: 23 additions & 0 deletions app/src/main/AndroidManifest.xml
@@ -0,0 +1,23 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="info.hossainkhan.recyclerviewdemo">

<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity
android:name=".MainActivity"
android:label="@string/app_name"
android:theme="@style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>

<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
</application>

</manifest>
@@ -0,0 +1,24 @@
package info.hossainkhan.recyclerviewdemo;


/**
* Part of code taken from
* https://github.com/googlesamples/android-RecyclerView/blob/master/Application/src/main/java/com/example/android/recyclerview/RecyclerViewFragment.java#L154
*/
public class DataSetProvider {

private static final int DATASET_COUNT = 30;

/**
* Generates Strings for RecyclerView's adapter. This data would usually come
* from a local content provider or remote server.
*/
public static String[] generateDataset() {
String[] mDataset = new String[DATASET_COUNT];
for (int i = 0; i < DATASET_COUNT; i++) {
mDataset[i] = "This is element #" + i;
}

return mDataset;
}
}
134 changes: 134 additions & 0 deletions app/src/main/java/info/hossainkhan/recyclerviewdemo/MainActivity.java
@@ -0,0 +1,134 @@
package info.hossainkhan.recyclerviewdemo;


import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.support.design.widget.NavigationView;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener {

public static final String GITHUB_PROJECT_URL = "https://github.com/amardeshbd/android-recycler-view-wrap-content";

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);

FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});

DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.setDrawerListener(toggle);
toggle.syncState();

NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
}

@Override
protected void onStart() {
super.onStart();
Toast.makeText(this, "Select a demo view from navigation bar to test " +
"`RecyclerView` wrap_content issue inside `ScrollView`.", Toast.LENGTH_LONG).show();
}

@Override
public void onBackPressed() {
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
} else {
super.onBackPressed();
}
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();

//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}

return super.onOptionsItemSelected(item);
}

@SuppressWarnings("StatementWithEmptyBody")
@Override
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
int id = item.getItemId();

if (id == R.id.demo_without_fix) {
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
//You can then add a fragment using the add() method, specifying the fragment to add and the view in which to insert it. For example:
RecyclerViewWrapContentIssueDemoFragment fragment = new RecyclerViewWrapContentIssueDemoFragment();

fragmentTransaction.replace(R.id.content_main, fragment);
fragmentTransaction.commit();
} else if (id == R.id.demo_with_fix) {
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
//You can then add a fragment using the add() method, specifying the fragment to add and the view in which to insert it. For example:
RecyclerViewWrapContentFixDemoFragment fragment = new RecyclerViewWrapContentFixDemoFragment();

fragmentTransaction.replace(R.id.content_main, fragment);
fragmentTransaction.commit();

} else if (id == R.id.nav_share) {
// https://developer.android.com/training/sharing/send.html
Intent sendIntent = new Intent();
sendIntent.setAction(Intent.ACTION_SEND);
sendIntent.putExtra(Intent.EXTRA_TEXT, "Checkout sample project on RecyclerView wrap content workaround: " + GITHUB_PROJECT_URL);
sendIntent.setType("text/plain");
startActivity(Intent.createChooser(sendIntent, "Share..."));

} else if (id == R.id.nav_send) {
String url = GITHUB_PROJECT_URL;
Intent i = new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse(url));
startActivity(i);
}

DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
}
@@ -0,0 +1,47 @@
package info.hossainkhan.recyclerviewdemo;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class RecyclerViewWrapContentFixDemoFragment extends Fragment {

private RecyclerView mRecyclerView;
private LinearLayoutManager mLayoutManager;
private SimpleListAdapter mAdapter;

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_recycler_view_with_fix, container, false);
mRecyclerView = (RecyclerView) view.findViewById(R.id.my_recycler_view);
return view;
}

@Override
public void onStart() {
super.onStart();

// Partial code from https://developer.android.com/training/material/lists-cards.html#RecyclerView

// use this setting to improve performance if you know that changes
// in content do not change the layout size of the RecyclerView
mRecyclerView.setHasFixedSize(true);

// use a linear layout manager
mLayoutManager = new LinearLayoutManager(getContext());
mLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
mRecyclerView.setLayoutManager(mLayoutManager);

mRecyclerView.setNestedScrollingEnabled(false);

// specify an adapter (see also next example)
mAdapter = new SimpleListAdapter(DataSetProvider.generateDataset());
mRecyclerView.setAdapter(mAdapter);
}
}
@@ -0,0 +1,47 @@
package info.hossainkhan.recyclerviewdemo;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class RecyclerViewWrapContentIssueDemoFragment extends Fragment {

private RecyclerView mRecyclerView;
private LinearLayoutManager mLayoutManager;
private SimpleListAdapter mAdapter;

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_recycler_view_without_fix, container, false);
mRecyclerView = (RecyclerView) view.findViewById(R.id.my_recycler_view);
return view;
}

@Override
public void onStart() {
super.onStart();

// Partial code from https://developer.android.com/training/material/lists-cards.html#RecyclerView

// use this setting to improve performance if you know that changes
// in content do not change the layout size of the RecyclerView
mRecyclerView.setHasFixedSize(true);

// use a linear layout manager
mLayoutManager = new LinearLayoutManager(getContext());
mLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
mRecyclerView.setLayoutManager(mLayoutManager);

mRecyclerView.setNestedScrollingEnabled(false);

// specify an adapter (see also next example)
mAdapter = new SimpleListAdapter(DataSetProvider.generateDataset());
mRecyclerView.setAdapter(mAdapter);
}
}

0 comments on commit dc0a997

Please sign in to comment.