diff options
author | Arne Schwabe <arne@rfc2549.org> | 2014-11-16 21:18:24 +0100 |
---|---|---|
committer | Arne Schwabe <arne@rfc2549.org> | 2014-11-16 21:18:24 +0100 |
commit | f4a711c94201c1dc0fc1f38c5513c8c52056f078 (patch) | |
tree | 02a63f9b3fee6f5dab83eeed37e704976bacd1cc | |
parent | 08f9700058b07b79908110baf4cef353d7eba5f4 (diff) |
Change Connection Container to RecyclerView
--HG--
extra : rebase_source : 941124b2fbb4904470a015e879deed74ae9972ad
4 files changed, 95 insertions, 80 deletions
diff --git a/main/src/main/java/de/blinkt/openvpn/fragments/ConnectionsAdapter.java b/main/src/main/java/de/blinkt/openvpn/fragments/ConnectionsAdapter.java index 3d20db73..94247fab 100644 --- a/main/src/main/java/de/blinkt/openvpn/fragments/ConnectionsAdapter.java +++ b/main/src/main/java/de/blinkt/openvpn/fragments/ConnectionsAdapter.java @@ -6,6 +6,7 @@ package de.blinkt.openvpn.fragments; import android.content.Context; +import android.support.v7.widget.RecyclerView; import android.text.Editable; import android.text.TextWatcher; import android.view.LayoutInflater; @@ -28,7 +29,7 @@ import de.blinkt.openvpn.core.Connection; /** * Created by arne on 30.10.14. */ -public class ConnectionsAdapter extends BaseAdapter { +public class ConnectionsAdapter extends RecyclerView.Adapter<ConnectionsAdapter.ConnectionsHolder> { private final Context mContext; private final VpnProfile mProfile; private final Settings_Connections mConnectionFragment; @@ -42,44 +43,46 @@ public class ConnectionsAdapter extends BaseAdapter { mConnectionFragment = connections_fragments; } - @Override - public int getCount() { - return mConnections.length; - } + public static class ConnectionsHolder extends RecyclerView.ViewHolder { + private final EditText mServerNameView; + private final TextView mServerPortNumber; + private final EditText mPortNumberView; + private final Switch mRemoteSwitch; + private final RadioGroup mProtoGroup; + private final EditText mCustomOptionText; + private final CheckBox mCustomOptionCB; + private final View mCustomOptionsLayout; + + public ConnectionsHolder(View card) { + super(card); + mServerPortNumber = ((TextView)card.findViewById(R.id.portnumber)); + mServerNameView = (EditText) card.findViewById(R.id.servername); + mPortNumberView = (EditText) card.findViewById(R.id.portnumber); + mRemoteSwitch = (Switch) card.findViewById (R.id.remoteSwitch); + mCustomOptionCB = (CheckBox) card.findViewById(R.id.use_customoptions); + mCustomOptionText = (EditText) card.findViewById(R.id.customoptions); + mProtoGroup = (RadioGroup) card.findViewById(R.id.udptcpradiogroup); + mCustomOptionsLayout = card.findViewById(R.id.custom_options_layout); - @Override - public Object getItem(int position) { - return position; + } } @Override - public long getItemId(int position) { - return 0; + public ConnectionsAdapter.ConnectionsHolder onCreateViewHolder(ViewGroup viewGroup, int i) { + LayoutInflater li = LayoutInflater.from(mContext); + View card = li.inflate(R.layout.server_card, viewGroup, false); + + return new ConnectionsHolder(card); } - // create a new ImageView for each item referenced by the Adapter @Override - public View getView(int position, View convertView, ViewGroup parent) { - View card; - if (convertView==null) { - LayoutInflater li = LayoutInflater.from(mContext); - card = li.inflate(R.layout.server_card, parent, false); - } else { - card = convertView; - } - final Connection connection = mConnections[position]; - ((TextView)card.findViewById(R.id.portnumber)). - setText(connection.mServerPort); - - EditText serverNameView = (EditText) card.findViewById(R.id.servername); - EditText portNumberView = (EditText) card.findViewById(R.id.portnumber); - - serverNameView.setText(connection.mServerName); - portNumberView.setText(connection.mServerPort); - - Switch remoteSwitch = (Switch) card.findViewById (R.id.remoteSwitch); - remoteSwitch.setChecked(connection.mEnabled); - remoteSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { + public void onBindViewHolder(final ConnectionsAdapter.ConnectionsHolder cH, final int i) { + final Connection connection = mConnections[i]; + cH.mPortNumberView.setText(connection.mServerPort); + cH.mServerNameView.setText(connection.mServerName); + cH.mPortNumberView.setText(connection.mServerPort); + cH.mRemoteSwitch.setChecked(connection.mEnabled); + cH.mRemoteSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { connection.mEnabled = isChecked; @@ -88,7 +91,32 @@ public class ConnectionsAdapter extends BaseAdapter { }); - serverNameView.addTextChangedListener(new TextWatcher() { + cH.mProtoGroup.check(connection.mUseUdp ? R.id.udp_proto : R.id.tcp_proto); + cH.mProtoGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() { + @Override + public void onCheckedChanged(RadioGroup group, int checkedId) { + if (checkedId == R.id.udp_proto) + connection.mUseUdp=true; + else if (checkedId == R.id.tcp_proto) + connection.mUseUdp=false; + } + }); + + cH.mCustomOptionsLayout.setVisibility(connection.mUseCustomConfig ? View.VISIBLE : View.GONE); + cH.mCustomOptionText.setText(connection.mCustomConfiguration); + + cH.mCustomOptionCB.setChecked(connection.mUseCustomConfig); + cH.mCustomOptionCB.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { + @Override + public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) + { + connection.mUseCustomConfig = isChecked; + cH.mCustomOptionsLayout.setVisibility(connection.mUseCustomConfig ? View.VISIBLE : View.GONE); + notifyItemChanged(i); + } + }); + + cH.mServerNameView.addTextChangedListener(new TextWatcher() { @Override public void beforeTextChanged(CharSequence s, int start, int count, int after) { @@ -105,7 +133,7 @@ public class ConnectionsAdapter extends BaseAdapter { } }); - portNumberView.addTextChangedListener(new TextWatcher() { + cH.mPortNumberView.addTextChangedListener(new TextWatcher() { @Override public void beforeTextChanged(CharSequence s, int start, int count, int after) { @@ -122,39 +150,17 @@ public class ConnectionsAdapter extends BaseAdapter { } }); - CheckBox customOptionCB = (CheckBox) card.findViewById(R.id.use_customoptions); - final EditText editText = (EditText) card.findViewById(R.id.customoptions); - RadioGroup protoGroup = (RadioGroup) card.findViewById(R.id.udptcpradiogroup); - protoGroup.check(connection.mUseUdp ? R.id.udp_proto : R.id.tcp_proto); - protoGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() { - @Override - public void onCheckedChanged(RadioGroup group, int checkedId) { - if (checkedId == R.id.udp_proto) - connection.mUseUdp=true; - else if (checkedId == R.id.tcp_proto) - connection.mUseUdp=false; - } - }); - final View customOptionsLayout = card.findViewById(R.id.custom_options_layout); - customOptionsLayout.setVisibility(connection.mUseCustomConfig ? View.VISIBLE : View.GONE); - editText.setText(connection.mCustomConfiguration); + } - customOptionCB.setChecked(connection.mUseCustomConfig); - customOptionCB.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { - @Override - public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) - { - connection.mUseCustomConfig = isChecked; - customOptionsLayout.setVisibility(connection.mUseCustomConfig ? View.VISIBLE : View.GONE); - } - }); - displayWarningifNoneEnabled(); - return card; + @Override + public int getItemCount() { + return mConnections.length; } - private void displayWarningifNoneEnabled() { + private void displayWarningifNoneEnabled() + { int showWarning = View.VISIBLE; for(Connection conn:mConnections) { if(conn.mEnabled) @@ -166,8 +172,8 @@ public class ConnectionsAdapter extends BaseAdapter { public void addRemote() { mConnections = Arrays.copyOf(mConnections, mConnections.length+1); mConnections[mConnections.length-1] = new Connection(); + notifyItemInserted(mConnections.length-1); - notifyDataSetInvalidated(); } public void saveProfile() { diff --git a/main/src/main/java/de/blinkt/openvpn/fragments/Settings_Connections.java b/main/src/main/java/de/blinkt/openvpn/fragments/Settings_Connections.java index 435bdb26..60008136 100644 --- a/main/src/main/java/de/blinkt/openvpn/fragments/Settings_Connections.java +++ b/main/src/main/java/de/blinkt/openvpn/fragments/Settings_Connections.java @@ -9,6 +9,9 @@ import android.app.Fragment; import android.os.Build; import android.os.Bundle; import android.support.annotation.Nullable; +import android.support.v7.widget.LinearLayoutManager; +import android.support.v7.widget.RecyclerView; +import android.support.v7.widget.StaggeredGridLayoutManager; import android.view.LayoutInflater; import android.view.Menu; import android.view.MenuInflater; @@ -33,6 +36,7 @@ public class Settings_Connections extends Fragment implements View.OnClickListen private ConnectionsAdapter mConnectionsAdapter; private TextView mWarning; private CheckBox mUseRandomRemote; + private RecyclerView mRecyclerView; @Override public void onCreate(Bundle savedInstanceState) { @@ -56,16 +60,18 @@ public class Settings_Connections extends Fragment implements View.OnClickListen public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View v = inflater.inflate(R.layout.connections, container, false); - GridView gridview = (GridView) v.findViewById(R.id.gridview); - gridview.setOnItemClickListener(new AdapterView.OnItemClickListener() { - public void onItemClick(AdapterView<?> parent, View v, int position, long id) { - Toast.makeText(getActivity(), "" + position, Toast.LENGTH_SHORT).show(); - } - }); + mRecyclerView = (RecyclerView) v.findViewById(R.id.connection_recycler_view); + + int dpwidth = (int) (container.getWidth()/getResources().getDisplayMetrics().density); + int columns = dpwidth/290; + columns = Math.max(1, columns); mConnectionsAdapter = new ConnectionsAdapter(getActivity(), this, mProfile); - gridview.setAdapter(mConnectionsAdapter); + + //mRecyclerView.setLayoutManager(new StaggeredGridLayoutManager(columns, StaggeredGridLayoutManager.VERTICAL)); + mRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity(),LinearLayoutManager.VERTICAL,false)); + mRecyclerView.setAdapter(mConnectionsAdapter); ImageButton fab_button = (ImageButton) v.findViewById(R.id.add_new_remote); if(fab_button!=null) diff --git a/main/src/main/res/layout/connections.xml b/main/src/main/res/layout/connections.xml index 30494bef..1c44d498 100644 --- a/main/src/main/res/layout/connections.xml +++ b/main/src/main/res/layout/connections.xml @@ -35,17 +35,14 @@ android:layout_width="wrap_content" android:layout_height="wrap_content" /> - <GridView + <android.support.v7.widget.RecyclerView + android:id="@+id/connection_recycler_view" android:layout_below="@id/remote_random" - android:id="@+id/gridview" android:layout_width="match_parent" android:layout_height="match_parent" - android:columnWidth="290dp" - android:gravity="center" - android:horizontalSpacing="0dp" - android:numColumns="auto_fit" - android:stretchMode="columnWidth" - android:verticalSpacing="10dp" /> + android:verticalSpacing="@dimen/stdpadding" + android:horizontalSpacing="@dimen/stdpadding" + /> <include layout="@layout/connection_fab" /> diff --git a/main/src/main/res/layout/server_card.xml b/main/src/main/res/layout/server_card.xml index d9051b29..96971d33 100644 --- a/main/src/main/res/layout/server_card.xml +++ b/main/src/main/res/layout/server_card.xml @@ -9,7 +9,7 @@ xmlns:card_view="http://schemas.android.com/apk/res-auto" android:layout_width="wrap_content" android:layout_height="wrap_content" - android:layout_margin="30dp" + android:layout_margin="@dimen/stdpadding" > <!-- A CardView that contains a TextView --> <android.support.v7.widget.CardView @@ -188,8 +188,14 @@ android:layout_alignRight="@+id/portnumber" android:layout_alignEnd="@+id/portnumber" /> - </RelativeLayout> + </android.support.v7.widget.CardView> + <ImageButton + android:layout_width="wrap_content" + android:layout_height="wrap_content" + android:id="@+id/imageButton" + android:layout_gravity="right|top" /> + </FrameLayout>
\ No newline at end of file |