I have a LinearLayout
(list_alphabet.xml), which contains a ListView
and a LinearLayout
.
The orientation is not explicitly set and therefore is horizontal.
I would expect the ListView
to appear on the left and then the 2nd 'LinearLayout' (which is vertical) to the right of it,
but in fact they are in reverse order.
The layout is added to a Fragment within MainActivity.
Please let me know if there is another piece of code to show that would be helpful.
How can I get them to appear in the order that they appear in the xml layout file?
MainActivity
public class MainActivity extends AppCompatActivity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.list_alphabet);
fragmentTransaction.add(R.id.list_alphabet_layout, new MyListFragment(), getResources().getString(R.string.list_fragment_tag));
fragmentTransaction.commit();
fragmentManager.executePendingTransactions();
...
}
list_alphabet.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/list_alphabet_layout"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<ListView
android:id="@android:id/list"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:fastScrollEnabled="true" />
<LinearLayout
android:id="@+id/sideIndex"
android:layout_width="60dip"
android:layout_height="fill_parent"
android:gravity="center_horizontal"
android:orientation="vertical" >
</LinearLayout>
</LinearLayout>
This is the Java code for the side index:
public void updateList() {
// setup the side index (the column of letters)
LinearLayout sideIndexLayout = (LinearLayout) getActivity().findViewById(R.id.sideIndex);
sideIndexLayout.removeAllViews();
indexListSize = alphabetList.size();
if (indexListSize < 1) {
return;
}
int indexMaxSize = (int) Math.floor(sideIndexLayout.getHeight() / 20);
int tmpIndexListSize = indexListSize;
while (tmpIndexListSize > indexMaxSize) {
tmpIndexListSize = tmpIndexListSize / 2;
}
double delta;
if (tmpIndexListSize > 0) {
delta = indexListSize / tmpIndexListSize;
}
else {
delta = 1;
}
TextView tempTextView;
for (double i = 1; i <= indexListSize; i = i + delta) {
Object[] tmpIndexItem = alphabetList.get((int) i - 1);
String tmpLetter = tmpIndexItem[0].toString();
tempTextView = new TextView(getActivity());
tempTextView.setTextColor(getResources().getColor(android.R.color.white));
tempTextView.setText(tmpLetter);
tempTextView.setGravity(Gravity.CENTER);
tempTextView.setTextSize(15);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT, 1);
tempTextView.setLayoutParams(params);
sideIndexLayout.addView(tempTextView);
}
sideIndexHeight = sideIndexLayout.getHeight();
sideIndexLayout.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
sideIndexX = event.getX(); // coordinates of the touch
sideIndexY = event.getY();
// and can display a proper item in name list
displayListItem();
return false;
}
});
}
public void displayListItem() {
LinearLayout sideIndexLayout = (LinearLayout) getActivity().findViewById(R.id.sideIndex);
sideIndexLayout.setBackgroundColor(getResources().getColor(R.color.emnrd_green));
sideIndexHeight = sideIndexLayout.getHeight();
// compute number of pixels for every side index item
double pixelPerIndexItem = (double) sideIndexHeight / indexListSize;
// compute the item index for given event position belongs to
int itemPosition = (int) (sideIndexY / pixelPerIndexItem);
// get the item (we can do it since we know item index)
if (itemPosition < alphabetList.size()) {
Object[] indexItem = alphabetList.get(itemPosition);
int subitemPosition = sectionMap.get(indexItem[0]);
//ListView listView = (ListView) findViewById(android.R.id.list);
getListView().setSelection(subitemPosition);
}
}