Define repo_item layout in XML instead of run time

This commit is contained in:
relan 2015-09-22 16:26:17 +03:00
parent d3f7e9555e
commit 8b15fb4cb1
2 changed files with 38 additions and 46 deletions

View File

@ -1,8 +1,9 @@
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="25dp"
android:layout_height="?android:attr/listPreferredItemHeight"
android:orientation="horizontal"
android:gravity="center_vertical"
android:padding="8dp"
android:descendantFocusability="blocksDescendants">
<!--
@ -12,22 +13,39 @@
http://syedasaraahmed.wordpress.com/2012/10/03/android-onitemclicklistener-not-responding-clickable-rowitem-of-custom-listview/
-->
<TextView android:id="@+id/repo_name"
android:textSize="21sp"
android:textStyle="bold"
android:singleLine="true"
android:ellipsize="marquee"
android:layout_width="wrap_content"
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"/>
android:layout_weight="1"
android:orientation="vertical"
android:gravity="center_vertical">
<TextView android:id="@+id/repo_unsigned"
android:textSize="14sp"
<TextView
android:id="@+id/repo_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_vertical|start"
android:textSize="21sp"
android:textStyle="bold"
android:singleLine="true"
android:ellipsize="marquee"/>
<TextView
android:id="@+id/repo_unsigned"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_vertical|start"
android:text="@string/unsigned"
android:textSize="14sp"
android:textColor="@color/unsigned"
android:singleLine="true"
android:ellipsize="marquee"/>
</LinearLayout>
<android.support.v7.widget.SwitchCompat
android:id="@+id/repo_switch"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/repo_name"
android:text="@string/unsigned"
android:textColor="@color/unsigned"/>
android:layout_height="match_parent"/>
</RelativeLayout>
</LinearLayout>

View File

@ -7,12 +7,9 @@ import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CompoundButton;
import android.widget.RelativeLayout;
import android.widget.TextView;
import org.fdroid.fdroid.R;
import org.fdroid.fdroid.compat.LayoutCompat;
import org.fdroid.fdroid.compat.SwitchCompat;
import org.fdroid.fdroid.data.Repo;
public class RepoAdapter extends CursorAdapter {
@ -21,8 +18,6 @@ public class RepoAdapter extends CursorAdapter {
void onSetEnabled(Repo repo, boolean isEnabled);
}
private static final int SWITCH_ID = 10000;
private final LayoutInflater inflater;
private EnabledListener enabledListener;
@ -54,14 +49,13 @@ public class RepoAdapter extends CursorAdapter {
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
View view = inflater.inflate(R.layout.repo_item, parent, false);
CompoundButton switchView = addSwitchToView(view, context);
setupView(cursor, view, switchView);
setupView(cursor, view, (CompoundButton) view.findViewById(R.id.repo_switch));
return view;
}
@Override
public void bindView(View view, Context context, Cursor cursor) {
CompoundButton switchView = (CompoundButton)view.findViewById(SWITCH_ID);
CompoundButton switchView = (CompoundButton) view.findViewById(R.id.repo_switch);
// Remove old listener (because we are reusing this view, we don't want
// to invoke the listener for the last repo to use it - particularly
@ -71,9 +65,7 @@ public class RepoAdapter extends CursorAdapter {
setupView(cursor, view, switchView);
}
private void setupView(Cursor cursor, View view, CompoundButton switchView) {
final Repo repo = new Repo(cursor);
switchView.setChecked(repo.inuse);
@ -91,9 +83,6 @@ public class RepoAdapter extends CursorAdapter {
TextView nameView = (TextView)view.findViewById(R.id.repo_name);
nameView.setText(repo.getName());
RelativeLayout.LayoutParams nameViewLayout =
(RelativeLayout.LayoutParams)nameView.getLayoutParams();
nameViewLayout.addRule(LayoutCompat.RelativeLayout.START_OF, switchView.getId());
TextView signedView = (TextView) view.findViewById(R.id.repo_unsigned);
if (repo.isSigned()) {
@ -108,19 +97,4 @@ public class RepoAdapter extends CursorAdapter {
signedView.setVisibility(View.VISIBLE);
}
}
private CompoundButton addSwitchToView(View parent, Context context) {
SwitchCompat switchBuilder = SwitchCompat.create(context);
CompoundButton switchView = switchBuilder.createSwitch();
switchView.setId(SWITCH_ID);
RelativeLayout.LayoutParams layout = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT
);
layout.addRule(LayoutCompat.RelativeLayout.ALIGN_PARENT_END);
layout.addRule(RelativeLayout.CENTER_VERTICAL);
switchView.setLayoutParams(layout);
((RelativeLayout)parent).addView(switchView);
return switchView;
}
}